Skip to content

Commit

Permalink
Fix for handling CapturedDecl which was causing the failure of some O…
Browse files Browse the repository at this point in the history
…penMP test cases after upgrade.
  • Loading branch information
sulekhark committed Feb 9, 2021
1 parent 5b67a18 commit 2afbfb5
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
3 changes: 3 additions & 0 deletions clang/include/clang/AST/CanonBounds.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ namespace clang {
Result CompareInteger(unsigned I1, unsigned I2) const;
Result CompareRelativeBoundsClause(const RelativeBoundsClause *RC1,
const RelativeBoundsClause *RC2);
Result CompareLoc(const SourceLocation *SL1,
const SourceLocation *SL2) const;
Result CompareScope(const DeclContext *DC1, const DeclContext *DC2) const;

Result CompareImpl(const PredefinedExpr *E1, const PredefinedExpr *E2);
Expand Down Expand Up @@ -133,6 +135,7 @@ namespace clang {
/// \brief Compare declarations that may be used by expressions or
/// or types.
Result CompareDecl(const NamedDecl *D1, const NamedDecl *D2) const;
Result CompareDecl(const CapturedDecl *D1, const CapturedDecl *D2) const;
Result CompareType(QualType T1, QualType T2) const;
Result CompareTypeIgnoreCheckedness(QualType QT1, QualType QT2) const;
Result CompareTypeLexicographically(QualType QT1, QualType QT2) const;
Expand Down
61 changes: 60 additions & 1 deletion clang/lib/AST/CanonBounds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "clang/AST/Expr.h"
#include "clang/AST/PreorderAST.h"
#include "clang/AST/Stmt.h"
#include "clang/Basic/SourceManager.h"

using namespace clang;
using Result = Lexicographic::Result;
Expand Down Expand Up @@ -109,6 +110,46 @@ static Result ComparePointers(T *P1, T *P2, bool &ordered) {
return Result::LessThan;
}

// \brief Order two source locations based on the lexicographic
// ordering of filenames if the two source locationsi are in
// different source files, and based on line/column numbers if
// the two source locations are in the same source file.
Result
Lexicographic::CompareLoc(const SourceLocation *SL1,
const SourceLocation *SL2) const {
if (SL1 && SL2) {
if (SL1 == SL2)
return Result::Equal;
const SourceManager *SM = &Context.getSourceManager();
if (!SM) {
llvm_unreachable("unexpected null SourceManager");
return Result::LessThan;
}
const SourceLocation SLoc1 = SM->getSpellingLoc(*SL1);
const SourceLocation SLoc2 = SM->getSpellingLoc(*SL2);
const PresumedLoc PLoc1 = SM->getPresumedLoc(SLoc1);
const PresumedLoc PLoc2 = SM->getPresumedLoc(SLoc2);
if (PLoc1.isInvalid() || PLoc2.isInvalid()) {
llvm_unreachable("unexpected invalid source locations");
return Result::LessThan;
}
Result Cmp = TranslateInt(strcmp(PLoc1.getFilename(), PLoc2.getFilename()));
if (Cmp != Result::Equal)
return Cmp;

Cmp = TranslateInt((int)PLoc1.getLine() - (int)PLoc2.getLine());
if (Cmp != Result::Equal)
return Cmp;

Cmp = TranslateInt((int)PLoc1.getColumn() - (int)PLoc2.getColumn());
if (Cmp != Result::Equal)
return Cmp;
}

llvm_unreachable("unexpected source locations");
return Result::LessThan;
}

Result
Lexicographic::CompareScope(const DeclContext *DC1, const DeclContext *DC2) const {
DC1 = DC1->getPrimaryContext();
Expand All @@ -123,7 +164,11 @@ Lexicographic::CompareScope(const DeclContext *DC1, const DeclContext *DC2) cons

switch (DC1->getDeclKind()) {
case Decl::TranslationUnit: return Result::Equal;
case Decl::Captured: return Result::Equal;
case Decl::Captured: {
const CapturedDecl *CD1 = dyn_cast<CapturedDecl>(DC1);
const CapturedDecl *CD2 = dyn_cast<CapturedDecl>(DC2);
return CompareDecl(CD1, CD2);
}
case Decl::Function:
case Decl::Enum:
case Decl::Record: {
Expand All @@ -137,6 +182,20 @@ Lexicographic::CompareScope(const DeclContext *DC1, const DeclContext *DC2) cons
}
}

Result
Lexicographic::CompareDecl(const CapturedDecl *CD1,
const CapturedDecl *CD2) const {
Stmt *SList1 = CD1->getBody();
Stmt *SList2 = CD2->getBody();
if (SList1 && SList2) {
const SourceLocation SL1 = SList1->getSourceRange().getBegin();
const SourceLocation SL2 = SList2->getSourceRange().getBegin();
return CompareLoc(&SL1, &SL2);
}
llvm_unreachable("unexpected captured scope type");
return Result::LessThan;
}

Result
Lexicographic::CompareDecl(const NamedDecl *D1Arg, const NamedDecl *D2Arg) const {
const NamedDecl *D1 = dyn_cast<NamedDecl>(D1Arg->getCanonicalDecl());
Expand Down
32 changes: 32 additions & 0 deletions clang/test/CheckedC/static-checking/bounds-decl-checking.c
Original file line number Diff line number Diff line change
Expand Up @@ -690,3 +690,35 @@ void f85(_Ptr<struct st_80_arr> arr, int b) {
arr->c = b * b;
}
}

void f86(void) {
#pragma omp parallel
for (int i = 0; i < 10; ++i)
;
#pragma omp parallel
for (int i = 0; i < 10; ++i)
;
}

void f87(void) {
#pragma omp target
#pragma omp parallel
for (int i = 0; i < 10; ++i)
;
#pragma omp parallel
for (int i = 0; i < 10; ++i)
;
}

void f88(void) {
#pragma omp parallel
for (int i = 0; i < 10; ++i) ; for (int i = 0; i < 10; ++i) ; // exprected-warning {{for loop has empty body}} \
// expected-note {{put the semicolon on a separate line to silence this warning}} \
// expected-warning {{for loop has empty body}}
#pragma omp parallel
for (int i = 0; i < 10; ++i) ;
}

void f89(void) {
#pragma omp parallel
}

0 comments on commit 2afbfb5

Please sign in to comment.