Author: erichkeane
Date: 2025-04-24T07:43:16-07:00
New Revision: d859cb68836191cfa469d0c951134b53ee31298e

URL: 
https://github.com/llvm/llvm-project/commit/d859cb68836191cfa469d0c951134b53ee31298e
DIFF: 
https://github.com/llvm/llvm-project/commit/d859cb68836191cfa469d0c951134b53ee31298e.diff

LOG: [OpenACC] Fix variable dereference found by static analysis

Reported here: https://github.com/llvm/llvm-project/issues/137116

Fixes: 137116

Added: 
    

Modified: 
    clang/lib/Sema/SemaOpenACCClause.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaOpenACCClause.cpp 
b/clang/lib/Sema/SemaOpenACCClause.cpp
index fba40c834e703..6cf6888e2a3a9 100644
--- a/clang/lib/Sema/SemaOpenACCClause.cpp
+++ b/clang/lib/Sema/SemaOpenACCClause.cpp
@@ -2085,32 +2085,31 @@ bool 
SemaOpenACC::CheckDeclareClause(SemaOpenACC::OpenACCParsedClause &Clause,
       }
     } else {
       const auto *DRE = cast<DeclRefExpr>(VarExpr);
-      const VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl());
-      if (Var)
+      if (const auto *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
         CurDecl = Var->getCanonicalDecl();
 
-      // OpenACC3.3 2.13:
-      // A 'declare' directive must be in the same scope as the declaration of
-      // any var that appears in the clauses of the directive or any scope
-      // within a C/C++ function.
-      // We can't really check 'scope' here, so we check declaration context,
-      // which is a reasonable approximation, but misses scopes inside of
-      // functions.
-      if (removeLinkageSpecDC(Var->getCanonicalDecl()
-                                  ->getLexicalDeclContext()
-                                  ->getPrimaryContext()) != DC) {
-        Diag(VarExpr->getBeginLoc(), diag::err_acc_declare_same_scope)
-            << Clause.getClauseKind();
-        continue;
-      }
-      // OpenACC3.3 2.13:
-      // C and C++ extern variables may only appear in 'create',
-      // 'copyin', 'deviceptr', 'device_resident', or 'link' clauses on a
-      // 'declare' directive.
-      if (!IsSpecialClause && Var && Var->hasExternalStorage()) {
-        Diag(VarExpr->getBeginLoc(), diag::err_acc_declare_extern)
-            << Clause.getClauseKind();
-        continue;
+        // OpenACC3.3 2.13:
+        // A 'declare' directive must be in the same scope as the declaration 
of
+        // any var that appears in the clauses of the directive or any scope
+        // within a C/C++ function.
+        // We can't really check 'scope' here, so we check declaration context,
+        // which is a reasonable approximation, but misses scopes inside of
+        // functions.
+        if (removeLinkageSpecDC(
+                Var->getLexicalDeclContext()->getPrimaryContext()) != DC) {
+          Diag(VarExpr->getBeginLoc(), diag::err_acc_declare_same_scope)
+              << Clause.getClauseKind();
+          continue;
+        }
+        // OpenACC3.3 2.13:
+        // C and C++ extern variables may only appear in 'create',
+        // 'copyin', 'deviceptr', 'device_resident', or 'link' clauses on a
+        // 'declare' directive.
+        if (!IsSpecialClause && Var->hasExternalStorage()) {
+          Diag(VarExpr->getBeginLoc(), diag::err_acc_declare_extern)
+              << Clause.getClauseKind();
+          continue;
+        }
       }
 
       // OpenACC3.3 2.13:


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to