r361979 - [analyzer] print() JSONify: Type information implementation

Csaba Dabis via cfe-commits cfe-commits at lists.llvm.org
Wed May 29 08:53:12 PDT 2019


Author: charusso
Date: Wed May 29 08:53:12 2019
New Revision: 361979

URL: http://llvm.org/viewvc/llvm-project?rev=361979&view=rev
Log:
[analyzer] print() JSONify: Type information implementation

Summary: -

Reviewers: NoQ, xazax.hun, ravikandhadai, baloghadamsoftware, Szelethus

Reviewed By: NoQ

Subscribers: szepet, rnkovacs, a.sidorin, mikhail.ramalho, donat.nagy,
             dkrupp

Tags: #clang

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

Modified:
    cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h
    cfe/trunk/lib/StaticAnalyzer/Checkers/DynamicTypePropagation.cpp
    cfe/trunk/lib/StaticAnalyzer/Core/DynamicTypeMap.cpp
    cfe/trunk/lib/StaticAnalyzer/Core/ProgramState.cpp
    cfe/trunk/test/Analysis/expr-inspection.c

Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h?rev=361979&r1=361978&r2=361979&view=diff
==============================================================================
--- cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h (original)
+++ cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h Wed May 29 08:53:12 2019
@@ -29,12 +29,11 @@ class MemRegion;
 /// symbol to its most likely type.
 struct DynamicTypeMap {};
 
-using DynamicTypeMapImpl =
-    llvm::ImmutableMap<const MemRegion *, DynamicTypeInfo>;
+using DynamicTypeMapTy = llvm::ImmutableMap<const MemRegion *, DynamicTypeInfo>;
 
 template <>
 struct ProgramStateTrait<DynamicTypeMap>
-    : public ProgramStatePartialTrait<DynamicTypeMapImpl> {
+    : public ProgramStatePartialTrait<DynamicTypeMapTy> {
   static void *GDMIndex();
 };
 
@@ -54,8 +53,9 @@ inline ProgramStateRef setDynamicTypeInf
                             DynamicTypeInfo(NewTy, CanBeSubClassed));
 }
 
-void printDynamicTypeInfo(ProgramStateRef State, raw_ostream &Out,
-                          const char *NL, const char *Sep);
+void printDynamicTypeInfoJson(raw_ostream &Out, ProgramStateRef State,
+                              const char *NL = "\n", unsigned int Space = 0,
+                              bool IsDot = false);
 
 } // namespace ento
 } // namespace clang

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/DynamicTypePropagation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/DynamicTypePropagation.cpp?rev=361979&r1=361978&r2=361979&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/DynamicTypePropagation.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/DynamicTypePropagation.cpp Wed May 29 08:53:12 2019
@@ -114,8 +114,8 @@ public:
 void DynamicTypePropagation::checkDeadSymbols(SymbolReaper &SR,
                                               CheckerContext &C) const {
   ProgramStateRef State = C.getState();
-  DynamicTypeMapImpl TypeMap = State->get<DynamicTypeMap>();
-  for (DynamicTypeMapImpl::iterator I = TypeMap.begin(), E = TypeMap.end();
+  DynamicTypeMapTy TypeMap = State->get<DynamicTypeMap>();
+  for (DynamicTypeMapTy::iterator I = TypeMap.begin(), E = TypeMap.end();
        I != E; ++I) {
     if (!SR.isLiveRegion(I->first)) {
       State = State->remove<DynamicTypeMap>(I->first);

Modified: cfe/trunk/lib/StaticAnalyzer/Core/DynamicTypeMap.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/DynamicTypeMap.cpp?rev=361979&r1=361978&r2=361979&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/DynamicTypeMap.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/DynamicTypeMap.cpp Wed May 29 08:53:12 2019
@@ -13,6 +13,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h"
+#include "clang/Basic/JsonSupport.h"
 #include "clang/Basic/LLVM.h"
 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
@@ -53,27 +54,38 @@ ProgramStateRef setDynamicTypeInfo(Progr
   return NewState;
 }
 
-void printDynamicTypeInfo(ProgramStateRef State, raw_ostream &Out,
-                          const char *NL, const char *Sep) {
-  bool First = true;
-  for (const auto &I : State->get<DynamicTypeMap>()) {
-    if (First) {
-      Out << NL << "Dynamic types of regions:" << NL;
-      First = false;
-    }
-    const MemRegion *MR = I.first;
-    const DynamicTypeInfo &DTI = I.second;
-    Out << MR << " : ";
+void printDynamicTypeInfoJson(raw_ostream &Out, ProgramStateRef State,
+                              const char *NL, unsigned int Space, bool IsDot) {
+  Indent(Out, Space, IsDot) << "\"dynamic_types\": ";
+
+  const DynamicTypeMapTy &DTM = State->get<DynamicTypeMap>();
+  if (DTM.isEmpty()) {
+    Out << "null," << NL;
+    return;
+  }
+
+  ++Space;
+  Out << '[' << NL;
+  for (DynamicTypeMapTy::iterator I = DTM.begin(); I != DTM.end(); ++I) {
+    const MemRegion *MR = I->first;
+    const DynamicTypeInfo &DTI = I->second;
+    Out << "{ \"region\": \"" << MR << "\", \"dyn_type\": ";
     if (DTI.isValid()) {
-      Out << DTI.getType()->getPointeeType().getAsString();
-      if (DTI.canBeASubClass()) {
-        Out << " (or its subclass)";
-      }
+      Out << '\"' << DTI.getType()->getPointeeType().getAsString()
+          << "\" \"sub_classable\": "
+          << (DTI.canBeASubClass() ? "true" : "false");
     } else {
-      Out << "Invalid type info";
+      Out << "null"; // Invalid type info
     }
+    Out << "\" }";
+
+    if (std::next(I) != DTM.end())
+      Out << ',';
     Out << NL;
   }
+
+  --Space;
+  Indent(Out, Space, IsDot) << "]," << NL;
 }
 
 void *ProgramStateTrait<DynamicTypeMap>::GDMIndex() {

Modified: cfe/trunk/lib/StaticAnalyzer/Core/ProgramState.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/ProgramState.cpp?rev=361979&r1=361978&r2=361979&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/ProgramState.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/ProgramState.cpp Wed May 29 08:53:12 2019
@@ -455,7 +455,7 @@ void ProgramState::printJson(raw_ostream
   Mgr.getConstraintManager().printJson(Out, this, NL, Space, IsDot);
 
   // Print out the tracked dynamic types.
-  printDynamicTypeInfo(this, Out, NL, Sep);
+  printDynamicTypeInfoJson(Out, this, NL, Space, IsDot);
 
   // Print checker-specific data.
   Mgr.getOwningEngine().printState(Out, this, LCtx, NL, Space, IsDot);

Modified: cfe/trunk/test/Analysis/expr-inspection.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/expr-inspection.c?rev=361979&r1=361978&r2=361979&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/expr-inspection.c (original)
+++ cfe/trunk/test/Analysis/expr-inspection.c Wed May 29 08:53:12 2019
@@ -36,4 +36,4 @@ void foo(int x) {
 // CHECK-NEXT: "constraints": [
 // CHECK-NEXT:   { "symbol": "reg_$0<int x>", "range": "{ [-2147483648, 13] }" }
 // CHECK-NEXT: ],
-
+// CHECK-NEXT: "dynamic_types": null,




More information about the cfe-commits mailing list