Skip to content

[Clang][AST][NFC] Add assertion on Init to CompoundLiteralExpr #152593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 8, 2025

Conversation

shafik
Copy link
Collaborator

@shafik shafik commented Aug 7, 2025

Static analysis complained that:

child_range(&Init, &Init+1);

in the children member function was potentially out of bounds. This is false b/c it is forming an iterator range but it would be invalid if Init was a nullptr.

I add an assertion in the constructor for this and remove to FIXME checks that are related to this. I checked the various usages and we always valid the argument is not nullptr.

Static analysis complained that:

  child_range(&Init, &Init+1);

in the children member function was potentially out of bounds. This is false b/c
it is forming an iterator range but it would be invalid if Init was a nullptr.

I add an assertion in the constructor for this and remove to FIXME checks that
are related to this. I checked the various usages and we always valid the
argument is not nullptr.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 7, 2025

@llvm/pr-subscribers-clang

Author: Shafik Yaghmour (shafik)

Changes

Static analysis complained that:

child_range(&Init, &Init+1);

in the children member function was potentially out of bounds. This is false b/c it is forming an iterator range but it would be invalid if Init was a nullptr.

I add an assertion in the constructor for this and remove to FIXME checks that are related to this. I checked the various usages and we always valid the argument is not nullptr.


Full diff: https://github.com/llvm/llvm-project/pull/152593.diff

1 Files Affected:

  • (modified) clang/include/clang/AST/Expr.h (+1-6)
diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h
index 237b3b2cf7444..a5116a72264b9 100644
--- a/clang/include/clang/AST/Expr.h
+++ b/clang/include/clang/AST/Expr.h
@@ -3548,6 +3548,7 @@ class CompoundLiteralExpr : public Expr {
                       QualType T, ExprValueKind VK, Expr *init, bool fileScope)
       : Expr(CompoundLiteralExprClass, T, VK, OK_Ordinary),
         LParenLoc(lparenloc), TInfoAndScope(tinfo, fileScope), Init(init) {
+    assert(Init && "Init is a nullptr");
     setDependence(computeDependence(this));
   }
 
@@ -3577,17 +3578,11 @@ class CompoundLiteralExpr : public Expr {
   APValue &getStaticValue() const;
 
   SourceLocation getBeginLoc() const LLVM_READONLY {
-    // FIXME: Init should never be null.
-    if (!Init)
-      return SourceLocation();
     if (LParenLoc.isInvalid())
       return Init->getBeginLoc();
     return LParenLoc;
   }
   SourceLocation getEndLoc() const LLVM_READONLY {
-    // FIXME: Init should never be null.
-    if (!Init)
-      return SourceLocation();
     return Init->getEndLoc();
   }
 

Copy link

github-actions bot commented Aug 7, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@shafik shafik merged commit 2936774 into llvm:main Aug 8, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants