[PATCH] D87004: [analyzer] Evaluate PredefinedExpressions

Balázs Benics via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 2 02:30:35 PDT 2020


steakhal created this revision.
steakhal added reviewers: NoQ, Szelethus, vsavchenko, xazax.hun.
Herald added subscribers: cfe-commits, ASDenysPetrov, martong, Charusso, dkrupp, donat.nagy, mikhail.ramalho, a.sidorin, rnkovacs, szepet, baloghadamsoftware, whisperity.
Herald added a project: clang.
steakhal requested review of this revision.

We did not evaluate such expressions, just returned `unknown` for such cases.
After this patch, we will be able to access a unique value identifying a template instantiation.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D87004

Files:
  clang/lib/StaticAnalyzer/Core/Environment.cpp
  clang/lib/StaticAnalyzer/Core/SValBuilder.cpp
  clang/test/Analysis/eval-predefined-exprs.cpp


Index: clang/test/Analysis/eval-predefined-exprs.cpp
===================================================================
--- /dev/null
+++ clang/test/Analysis/eval-predefined-exprs.cpp
@@ -0,0 +1,24 @@
+// RUN: %clang_analyze_cc1 --std=c++17 -analyzer-checker=core,debug.ExprInspection -verify %s
+
+void clang_analyzer_dump(const char *);
+
+template <typename T, auto Value, typename U>
+void func(U param) {
+  clang_analyzer_dump(__func__);
+  clang_analyzer_dump(__FUNCTION__);
+  clang_analyzer_dump(__PRETTY_FUNCTION__);
+  // expected-warning at -3 {{&Element{"func",0 S64b,char}}}
+  // expected-warning at -3 {{&Element{"func",0 S64b,char}}}
+  // expected-warning at -3 {{&Element{"void func(U) [T = Class, Value = 42, U = char]",0 S64b,char}}}
+}
+
+void foo() {
+  clang_analyzer_dump(__func__);
+  clang_analyzer_dump(__FUNCTION__);
+  clang_analyzer_dump(__PRETTY_FUNCTION__);
+  // expected-warning at -3 {{&Element{"foo",0 S64b,char}}}
+  // expected-warning at -3 {{&Element{"foo",0 S64b,char}}}
+  // expected-warning at -3 {{&Element{"void foo()",0 S64b,char}}}
+
+  func<struct Class, 42ull>('b'); // instantiate template
+}
Index: clang/lib/StaticAnalyzer/Core/SValBuilder.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Core/SValBuilder.cpp
+++ clang/lib/StaticAnalyzer/Core/SValBuilder.cpp
@@ -306,6 +306,11 @@
     return makeLoc(getRegionManager().getStringRegion(SL));
   }
 
+  case Stmt::PredefinedExprClass: {
+    const auto *PE = cast<PredefinedExpr>(E);
+    return makeLoc(getRegionManager().getStringRegion(PE->getFunctionName()));
+  }
+
   // Fast-path some expressions to avoid the overhead of going through the AST's
   // constant evaluator
   case Stmt::CharacterLiteralClass: {
Index: clang/lib/StaticAnalyzer/Core/Environment.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Core/Environment.cpp
+++ clang/lib/StaticAnalyzer/Core/Environment.cpp
@@ -109,6 +109,7 @@
   case Stmt::StringLiteralClass:
   case Stmt::TypeTraitExprClass:
   case Stmt::SizeOfPackExprClass:
+  case Stmt::PredefinedExprClass:
     // Known constants; defer to SValBuilder.
     return svalBuilder.getConstantVal(cast<Expr>(S)).getValue();
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87004.289375.patch
Type: text/x-patch
Size: 2249 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200902/230e0e02/attachment.bin>


More information about the cfe-commits mailing list