Skip to content

Commit d116b39

Browse files
erichkeaneIanWood1
authored andcommitted
[OpenACC] Fix variable dereference found by static analysis
Reported here: llvm#137116 Fixes: 137116
1 parent 90829d5 commit d116b39

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

clang/lib/Sema/SemaOpenACCClause.cpp

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,32 +2085,31 @@ bool SemaOpenACC::CheckDeclareClause(SemaOpenACC::OpenACCParsedClause &Clause,
20852085
}
20862086
} else {
20872087
const auto *DRE = cast<DeclRefExpr>(VarExpr);
2088-
const VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl());
2089-
if (Var)
2088+
if (const auto *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
20902089
CurDecl = Var->getCanonicalDecl();
20912090

2092-
// OpenACC3.3 2.13:
2093-
// A 'declare' directive must be in the same scope as the declaration of
2094-
// any var that appears in the clauses of the directive or any scope
2095-
// within a C/C++ function.
2096-
// We can't really check 'scope' here, so we check declaration context,
2097-
// which is a reasonable approximation, but misses scopes inside of
2098-
// functions.
2099-
if (removeLinkageSpecDC(Var->getCanonicalDecl()
2100-
->getLexicalDeclContext()
2101-
->getPrimaryContext()) != DC) {
2102-
Diag(VarExpr->getBeginLoc(), diag::err_acc_declare_same_scope)
2103-
<< Clause.getClauseKind();
2104-
continue;
2105-
}
2106-
// OpenACC3.3 2.13:
2107-
// C and C++ extern variables may only appear in 'create',
2108-
// 'copyin', 'deviceptr', 'device_resident', or 'link' clauses on a
2109-
// 'declare' directive.
2110-
if (!IsSpecialClause && Var && Var->hasExternalStorage()) {
2111-
Diag(VarExpr->getBeginLoc(), diag::err_acc_declare_extern)
2112-
<< Clause.getClauseKind();
2113-
continue;
2091+
// OpenACC3.3 2.13:
2092+
// A 'declare' directive must be in the same scope as the declaration of
2093+
// any var that appears in the clauses of the directive or any scope
2094+
// within a C/C++ function.
2095+
// We can't really check 'scope' here, so we check declaration context,
2096+
// which is a reasonable approximation, but misses scopes inside of
2097+
// functions.
2098+
if (removeLinkageSpecDC(
2099+
Var->getLexicalDeclContext()->getPrimaryContext()) != DC) {
2100+
Diag(VarExpr->getBeginLoc(), diag::err_acc_declare_same_scope)
2101+
<< Clause.getClauseKind();
2102+
continue;
2103+
}
2104+
// OpenACC3.3 2.13:
2105+
// C and C++ extern variables may only appear in 'create',
2106+
// 'copyin', 'deviceptr', 'device_resident', or 'link' clauses on a
2107+
// 'declare' directive.
2108+
if (!IsSpecialClause && Var->hasExternalStorage()) {
2109+
Diag(VarExpr->getBeginLoc(), diag::err_acc_declare_extern)
2110+
<< Clause.getClauseKind();
2111+
continue;
2112+
}
21142113
}
21152114

21162115
// OpenACC3.3 2.13:

0 commit comments

Comments
 (0)