Skip to content

Commit 2afbfb5

Browse files
committed
Fix for handling CapturedDecl which was causing the failure of some OpenMP test cases after upgrade.
1 parent 5b67a18 commit 2afbfb5

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

clang/include/clang/AST/CanonBounds.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ namespace clang {
7676
Result CompareInteger(unsigned I1, unsigned I2) const;
7777
Result CompareRelativeBoundsClause(const RelativeBoundsClause *RC1,
7878
const RelativeBoundsClause *RC2);
79+
Result CompareLoc(const SourceLocation *SL1,
80+
const SourceLocation *SL2) const;
7981
Result CompareScope(const DeclContext *DC1, const DeclContext *DC2) const;
8082

8183
Result CompareImpl(const PredefinedExpr *E1, const PredefinedExpr *E2);
@@ -133,6 +135,7 @@ namespace clang {
133135
/// \brief Compare declarations that may be used by expressions or
134136
/// or types.
135137
Result CompareDecl(const NamedDecl *D1, const NamedDecl *D2) const;
138+
Result CompareDecl(const CapturedDecl *D1, const CapturedDecl *D2) const;
136139
Result CompareType(QualType T1, QualType T2) const;
137140
Result CompareTypeIgnoreCheckedness(QualType QT1, QualType QT2) const;
138141
Result CompareTypeLexicographically(QualType QT1, QualType QT2) const;

clang/lib/AST/CanonBounds.cpp

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "clang/AST/Expr.h"
2424
#include "clang/AST/PreorderAST.h"
2525
#include "clang/AST/Stmt.h"
26+
#include "clang/Basic/SourceManager.h"
2627

2728
using namespace clang;
2829
using Result = Lexicographic::Result;
@@ -109,6 +110,46 @@ static Result ComparePointers(T *P1, T *P2, bool &ordered) {
109110
return Result::LessThan;
110111
}
111112

113+
// \brief Order two source locations based on the lexicographic
114+
// ordering of filenames if the two source locationsi are in
115+
// different source files, and based on line/column numbers if
116+
// the two source locations are in the same source file.
117+
Result
118+
Lexicographic::CompareLoc(const SourceLocation *SL1,
119+
const SourceLocation *SL2) const {
120+
if (SL1 && SL2) {
121+
if (SL1 == SL2)
122+
return Result::Equal;
123+
const SourceManager *SM = &Context.getSourceManager();
124+
if (!SM) {
125+
llvm_unreachable("unexpected null SourceManager");
126+
return Result::LessThan;
127+
}
128+
const SourceLocation SLoc1 = SM->getSpellingLoc(*SL1);
129+
const SourceLocation SLoc2 = SM->getSpellingLoc(*SL2);
130+
const PresumedLoc PLoc1 = SM->getPresumedLoc(SLoc1);
131+
const PresumedLoc PLoc2 = SM->getPresumedLoc(SLoc2);
132+
if (PLoc1.isInvalid() || PLoc2.isInvalid()) {
133+
llvm_unreachable("unexpected invalid source locations");
134+
return Result::LessThan;
135+
}
136+
Result Cmp = TranslateInt(strcmp(PLoc1.getFilename(), PLoc2.getFilename()));
137+
if (Cmp != Result::Equal)
138+
return Cmp;
139+
140+
Cmp = TranslateInt((int)PLoc1.getLine() - (int)PLoc2.getLine());
141+
if (Cmp != Result::Equal)
142+
return Cmp;
143+
144+
Cmp = TranslateInt((int)PLoc1.getColumn() - (int)PLoc2.getColumn());
145+
if (Cmp != Result::Equal)
146+
return Cmp;
147+
}
148+
149+
llvm_unreachable("unexpected source locations");
150+
return Result::LessThan;
151+
}
152+
112153
Result
113154
Lexicographic::CompareScope(const DeclContext *DC1, const DeclContext *DC2) const {
114155
DC1 = DC1->getPrimaryContext();
@@ -123,7 +164,11 @@ Lexicographic::CompareScope(const DeclContext *DC1, const DeclContext *DC2) cons
123164

124165
switch (DC1->getDeclKind()) {
125166
case Decl::TranslationUnit: return Result::Equal;
126-
case Decl::Captured: return Result::Equal;
167+
case Decl::Captured: {
168+
const CapturedDecl *CD1 = dyn_cast<CapturedDecl>(DC1);
169+
const CapturedDecl *CD2 = dyn_cast<CapturedDecl>(DC2);
170+
return CompareDecl(CD1, CD2);
171+
}
127172
case Decl::Function:
128173
case Decl::Enum:
129174
case Decl::Record: {
@@ -137,6 +182,20 @@ Lexicographic::CompareScope(const DeclContext *DC1, const DeclContext *DC2) cons
137182
}
138183
}
139184

185+
Result
186+
Lexicographic::CompareDecl(const CapturedDecl *CD1,
187+
const CapturedDecl *CD2) const {
188+
Stmt *SList1 = CD1->getBody();
189+
Stmt *SList2 = CD2->getBody();
190+
if (SList1 && SList2) {
191+
const SourceLocation SL1 = SList1->getSourceRange().getBegin();
192+
const SourceLocation SL2 = SList2->getSourceRange().getBegin();
193+
return CompareLoc(&SL1, &SL2);
194+
}
195+
llvm_unreachable("unexpected captured scope type");
196+
return Result::LessThan;
197+
}
198+
140199
Result
141200
Lexicographic::CompareDecl(const NamedDecl *D1Arg, const NamedDecl *D2Arg) const {
142201
const NamedDecl *D1 = dyn_cast<NamedDecl>(D1Arg->getCanonicalDecl());

clang/test/CheckedC/static-checking/bounds-decl-checking.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,3 +690,35 @@ void f85(_Ptr<struct st_80_arr> arr, int b) {
690690
arr->c = b * b;
691691
}
692692
}
693+
694+
void f86(void) {
695+
#pragma omp parallel
696+
for (int i = 0; i < 10; ++i)
697+
;
698+
#pragma omp parallel
699+
for (int i = 0; i < 10; ++i)
700+
;
701+
}
702+
703+
void f87(void) {
704+
#pragma omp target
705+
#pragma omp parallel
706+
for (int i = 0; i < 10; ++i)
707+
;
708+
#pragma omp parallel
709+
for (int i = 0; i < 10; ++i)
710+
;
711+
}
712+
713+
void f88(void) {
714+
#pragma omp parallel
715+
for (int i = 0; i < 10; ++i) ; for (int i = 0; i < 10; ++i) ; // exprected-warning {{for loop has empty body}} \
716+
// expected-note {{put the semicolon on a separate line to silence this warning}} \
717+
// expected-warning {{for loop has empty body}}
718+
#pragma omp parallel
719+
for (int i = 0; i < 10; ++i) ;
720+
}
721+
722+
void f89(void) {
723+
#pragma omp parallel
724+
}

0 commit comments

Comments
 (0)