Skip to content

Conversation

@rniwa
Copy link
Contributor

@rniwa rniwa commented Feb 13, 2025

No description provided.

@rniwa rniwa requested review from haoNoQ and t-rasmud February 13, 2025 01:53
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels Feb 13, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 13, 2025

@llvm/pr-subscribers-clang-static-analyzer-1

Author: Ryosuke Niwa (rniwa)

Changes

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

2 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp (+5)
  • (modified) clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp (+4)
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
index d40b4b4dbb560..b823e048a4d10 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
@@ -503,6 +503,11 @@ class TrivialFunctionAnalysisVisitor
     return true;
   }
 
+  bool VisitOffsetOfExpr(const OffsetOfExpr* OE) {
+      // offsetof(T) is considered trivial.
+      return true;
+  }
+
   bool VisitCXXMemberCallExpr(const CXXMemberCallExpr *MCE) {
     if (!checkArguments(MCE))
       return false;
diff --git a/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp b/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp
index ffeecbf87c451..cf403851b74fd 100644
--- a/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp
@@ -77,6 +77,8 @@ T&& forward(T& arg);
 template<typename T>
 T&& move( T&& t );
 
+#define offsetof(t, d) __builtin_offsetof(t, d)
+
 } // namespace std
 
 bool isMainThread();
@@ -373,6 +375,7 @@ class RefCounted {
     double y;
   };
   void trivial68() { point pt = { 1.0 }; }
+  unsigned trivial69() { return offsetof(RefCounted, children); }
 
   static RefCounted& singleton() {
     static RefCounted s_RefCounted;
@@ -560,6 +563,7 @@ class UnrelatedClass {
     getFieldTrivial().trivial66()->trivial6(); // no-warning
     getFieldTrivial().trivial67()->trivial6(); // no-warning
     getFieldTrivial().trivial68(); // no-warning
+    getFieldTrivial().trivial69(); // no-warning
 
     RefCounted::singleton().trivial18(); // no-warning
     RefCounted::singleton().someFunction(); // no-warning

@llvmbot
Copy link
Member

llvmbot commented Feb 13, 2025

@llvm/pr-subscribers-clang

Author: Ryosuke Niwa (rniwa)

Changes

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

2 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp (+5)
  • (modified) clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp (+4)
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
index d40b4b4dbb560..b823e048a4d10 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
@@ -503,6 +503,11 @@ class TrivialFunctionAnalysisVisitor
     return true;
   }
 
+  bool VisitOffsetOfExpr(const OffsetOfExpr* OE) {
+      // offsetof(T) is considered trivial.
+      return true;
+  }
+
   bool VisitCXXMemberCallExpr(const CXXMemberCallExpr *MCE) {
     if (!checkArguments(MCE))
       return false;
diff --git a/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp b/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp
index ffeecbf87c451..cf403851b74fd 100644
--- a/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp
@@ -77,6 +77,8 @@ T&& forward(T& arg);
 template<typename T>
 T&& move( T&& t );
 
+#define offsetof(t, d) __builtin_offsetof(t, d)
+
 } // namespace std
 
 bool isMainThread();
@@ -373,6 +375,7 @@ class RefCounted {
     double y;
   };
   void trivial68() { point pt = { 1.0 }; }
+  unsigned trivial69() { return offsetof(RefCounted, children); }
 
   static RefCounted& singleton() {
     static RefCounted s_RefCounted;
@@ -560,6 +563,7 @@ class UnrelatedClass {
     getFieldTrivial().trivial66()->trivial6(); // no-warning
     getFieldTrivial().trivial67()->trivial6(); // no-warning
     getFieldTrivial().trivial68(); // no-warning
+    getFieldTrivial().trivial69(); // no-warning
 
     RefCounted::singleton().trivial18(); // no-warning
     RefCounted::singleton().someFunction(); // no-warning

@github-actions
Copy link

github-actions bot commented Feb 13, 2025

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

Copy link
Contributor

@t-rasmud t-rasmud left a comment

Choose a reason for hiding this comment

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

LGTM!

@rniwa
Copy link
Contributor Author

rniwa commented Feb 14, 2025

Thanks for the review!

@rniwa rniwa merged commit 18f60bc into llvm:main Feb 14, 2025
8 checks passed
@rniwa rniwa deleted the treat-offsetof-as-trivial branch February 14, 2025 21:47
rniwa added a commit to rniwa/llvm-project that referenced this pull request Feb 15, 2025
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:static analyzer clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants