Skip to content

Conversation

@fmayer
Copy link
Contributor

@fmayer fmayer commented Oct 22, 2025

This is useful to make sure that a newly created StorageLocation always
has e.g. an uninitialized boolean.

Created using spr 1.3.7
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:dataflow Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html clang:analysis labels Oct 22, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 22, 2025

@llvm/pr-subscribers-clang-analysis

@llvm/pr-subscribers-clang

Author: Florian Mayer (fmayer)

Changes

This is useful to make sure that a newly created StorageLocation always
has e.g. an uninitialized boolean.


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

2 Files Affected:

  • (modified) clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h (+7)
  • (modified) clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp (+7-2)
diff --git a/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h b/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
index 11042e865c4e6..57dfabaa16212 100644
--- a/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
+++ b/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
@@ -111,6 +111,12 @@ class DataflowAnalysisContext {
     SyntheticFieldCallback = CB;
   }
 
+  void setInitializerCallback(
+      std::function<void(QualType, StorageLocation&)> CB) {
+    assert(!RecordStorageLocationCreated);
+    InitializerCallback = CB;
+  }
+
   /// Returns a new storage location appropriate for `Type`.
   ///
   /// A null `Type` is interpreted as the pointee type of `std::nullptr_t`.
@@ -332,6 +338,7 @@ class DataflowAnalysisContext {
   std::unique_ptr<Logger> LogOwner; // If created via flags.
 
   std::function<llvm::StringMap<QualType>(QualType)> SyntheticFieldCallback;
+  std::optional<std::function<void(QualType, StorageLocation&)>> InitializerCallback;
 
   /// Has any `RecordStorageLocation` been created yet?
   bool RecordStorageLocationCreated = false;
diff --git a/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp b/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
index 4196d6821c184..1b1b41f1f3160 100644
--- a/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
+++ b/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
@@ -59,6 +59,7 @@ void DataflowAnalysisContext::addModeledFields(const FieldSet &Fields) {
 }
 
 StorageLocation &DataflowAnalysisContext::createStorageLocation(QualType Type) {
+  StorageLocation *S;
   if (!Type.isNull() && Type->isRecordType()) {
     llvm::DenseMap<const ValueDecl *, StorageLocation *> FieldLocs;
     for (const FieldDecl *Field : getModeledFields(Type))
@@ -74,10 +75,14 @@ StorageLocation &DataflowAnalysisContext::createStorageLocation(QualType Type) {
           {Entry.getKey(),
            &createStorageLocation(Entry.getValue().getNonReferenceType())});
 
-    return createRecordStorageLocation(Type, std::move(FieldLocs),
+    S = &createRecordStorageLocation(Type, std::move(FieldLocs),
                                        std::move(SyntheticFields));
+  } else {
+    S = &arena().create<ScalarStorageLocation>(Type);
   }
-  return arena().create<ScalarStorageLocation>(Type);
+  if (InitializerCallback)
+    (*InitializerCallback)(Type, *S);
+  return *S;
 }
 
 // Returns the keys for a given `StringMap`.

@github-actions
Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff origin/main HEAD --extensions h,cpp -- clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp --diff_from_common_commit

⚠️
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing origin/main to the base branch/commit you want to compare against.
⚠️

View the diff from clang-format here.
diff --git a/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h b/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
index 57dfabaa1..af9421060 100644
--- a/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
+++ b/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
@@ -111,8 +111,8 @@ public:
     SyntheticFieldCallback = CB;
   }
 
-  void setInitializerCallback(
-      std::function<void(QualType, StorageLocation&)> CB) {
+  void
+  setInitializerCallback(std::function<void(QualType, StorageLocation &)> CB) {
     assert(!RecordStorageLocationCreated);
     InitializerCallback = CB;
   }
@@ -338,7 +338,8 @@ private:
   std::unique_ptr<Logger> LogOwner; // If created via flags.
 
   std::function<llvm::StringMap<QualType>(QualType)> SyntheticFieldCallback;
-  std::optional<std::function<void(QualType, StorageLocation&)>> InitializerCallback;
+  std::optional<std::function<void(QualType, StorageLocation &)>>
+      InitializerCallback;
 
   /// Has any `RecordStorageLocation` been created yet?
   bool RecordStorageLocationCreated = false;
diff --git a/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp b/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
index 1b1b41f1f..e161f9689 100644
--- a/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
+++ b/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
@@ -76,7 +76,7 @@ StorageLocation &DataflowAnalysisContext::createStorageLocation(QualType Type) {
            &createStorageLocation(Entry.getValue().getNonReferenceType())});
 
     S = &createRecordStorageLocation(Type, std::move(FieldLocs),
-                                       std::move(SyntheticFields));
+                                     std::move(SyntheticFields));
   } else {
     S = &arena().create<ScalarStorageLocation>(Type);
   }

@fmayer fmayer marked this pull request as draft October 24, 2025 22:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:analysis clang:dataflow Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants