r365133 - [CTU] Add support for virtual functions

Gabor Marton via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 4 04:39:01 PDT 2019


Author: martong
Date: Thu Jul  4 04:39:00 2019
New Revision: 365133

URL: http://llvm.org/viewvc/llvm-project?rev=365133&view=rev
Log:
[CTU] Add support for virtual functions

Reviewers: Szelethus, xazax.hun

Subscribers: rnkovacs, dkrupp, gamesh411, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D63920

Modified:
    cfe/trunk/lib/StaticAnalyzer/Core/CallEvent.cpp
    cfe/trunk/test/Analysis/Inputs/ctu-other.cpp
    cfe/trunk/test/Analysis/Inputs/ctu-other.cpp.externalDefMap.txt
    cfe/trunk/test/Analysis/ctu-main.cpp

Modified: cfe/trunk/lib/StaticAnalyzer/Core/CallEvent.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/CallEvent.cpp?rev=365133&r1=365132&r2=365133&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/CallEvent.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/CallEvent.cpp Thu Jul  4 04:39:00 2019
@@ -766,8 +766,11 @@ RuntimeDefinition CXXInstanceCall::getRu
 
   // Does the decl that we found have an implementation?
   const FunctionDecl *Definition;
-  if (!Result->hasBody(Definition))
+  if (!Result->hasBody(Definition)) {
+    if (!DynType.canBeASubClass())
+      return AnyFunctionCall::getRuntimeDefinition();
     return {};
+  }
 
   // We found a definition. If we're not sure that this devirtualization is
   // actually what will happen at runtime, make sure to provide the region so

Modified: cfe/trunk/test/Analysis/Inputs/ctu-other.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/Inputs/ctu-other.cpp?rev=365133&r1=365132&r2=365133&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/Inputs/ctu-other.cpp (original)
+++ cfe/trunk/test/Analysis/Inputs/ctu-other.cpp Thu Jul  4 04:39:00 2019
@@ -38,6 +38,7 @@ int embed_cls::fecl(int x) {
 class mycls {
 public:
   int fcl(int x);
+  virtual int fvcl(int x);
   static int fscl(int x);
 
   class embed_cls2 {
@@ -49,6 +50,9 @@ public:
 int mycls::fcl(int x) {
   return x + 5;
 }
+int mycls::fvcl(int x) {
+  return x + 7;
+}
 int mycls::fscl(int x) {
   return x + 6;
 }
@@ -56,6 +60,15 @@ int mycls::embed_cls2::fecl2(int x) {
   return x - 11;
 }
 
+class derived : public mycls {
+public:
+  virtual int fvcl(int x) override;
+};
+
+int derived::fvcl(int x) {
+  return x + 8;
+}
+
 namespace chns {
 int chf2(int x);
 

Modified: cfe/trunk/test/Analysis/Inputs/ctu-other.cpp.externalDefMap.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/Inputs/ctu-other.cpp.externalDefMap.txt?rev=365133&r1=365132&r2=365133&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/Inputs/ctu-other.cpp.externalDefMap.txt (original)
+++ cfe/trunk/test/Analysis/Inputs/ctu-other.cpp.externalDefMap.txt Thu Jul  4 04:39:00 2019
@@ -3,8 +3,10 @@ c:@N at myns@N at embed_ns@F at fens#I# ctu-other
 c:@F at g#I# ctu-other.cpp.ast
 c:@S at mycls@F at fscl#I#S ctu-other.cpp.ast
 c:@S at mycls@F at fcl#I# ctu-other.cpp.ast
+c:@S at mycls@F at fvcl#I# ctu-other.cpp.ast
 c:@N at myns@S at embed_cls@F at fecl#I# ctu-other.cpp.ast
 c:@S at mycls@S at embed_cls2@F at fecl2#I# ctu-other.cpp.ast
+c:@S at derived@F at fvcl#I# ctu-other.cpp.ast
 c:@F at f#I# ctu-other.cpp.ast
 c:@N at myns@F at fns#I# ctu-other.cpp.ast
 c:@F at h#I# ctu-other.cpp.ast

Modified: cfe/trunk/test/Analysis/ctu-main.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/ctu-main.cpp?rev=365133&r1=365132&r2=365133&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/ctu-main.cpp (original)
+++ cfe/trunk/test/Analysis/ctu-main.cpp Thu Jul  4 04:39:00 2019
@@ -45,6 +45,7 @@ public:
 class mycls {
 public:
   int fcl(int x);
+  virtual int fvcl(int x);
   static int fscl(int x);
 
   class embed_cls2 {
@@ -53,6 +54,11 @@ public:
   };
 };
 
+class derived : public mycls {
+public:
+  virtual int fvcl(int x) override;
+};
+
 namespace chns {
 int chf1(int x);
 }
@@ -98,6 +104,14 @@ union U {
 };
 extern U extU;
 
+void test_virtual_functions(mycls* obj) {
+  // The dynamic type is known.
+  clang_analyzer_eval(mycls().fvcl(1) == 8);   // expected-warning{{TRUE}}
+  clang_analyzer_eval(derived().fvcl(1) == 9); // expected-warning{{TRUE}}
+  // We cannot decide about the dynamic type.
+  clang_analyzer_eval(obj->fvcl(1) == 8);      // expected-warning{{FALSE}} expected-warning{{TRUE}}
+}
+
 int main() {
   clang_analyzer_eval(f(3) == 2); // expected-warning{{TRUE}}
   clang_analyzer_eval(f(4) == 3); // expected-warning{{TRUE}}
@@ -116,7 +130,7 @@ int main() {
   clang_analyzer_eval(fun_using_anon_struct(8) == 8); // expected-warning{{TRUE}}
 
   clang_analyzer_eval(other_macro_diag(1) == 1); // expected-warning{{TRUE}}
-  // expected-warning at Inputs/ctu-other.cpp:80{{REACHABLE}}
+  // expected-warning at Inputs/ctu-other.cpp:93{{REACHABLE}}
   MACRODIAG(); // expected-warning{{REACHABLE}}
 
   clang_analyzer_eval(extInt == 2); // expected-warning{{TRUE}}




More information about the cfe-commits mailing list