[Lldb-commits] [lldb] r354301 - [lldb-instr] Wrap returns of struct/classes in LLDB_RECORD_RESULT

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Mon Feb 18 17:04:32 PST 2019


Author: jdevlieghere
Date: Mon Feb 18 17:04:31 2019
New Revision: 354301

URL: http://llvm.org/viewvc/llvm-project?rev=354301&view=rev
Log:
[lldb-instr] Wrap returns of struct/classes in LLDB_RECORD_RESULT

The instrumentation framework requires return values of custom classes
and structs to be wrapped in the LLDB_RECORD_RESULT macro.

Modified:
    lldb/trunk/lit/tools/lldb-instr/Inputs/foo.cpp
    lldb/trunk/lit/tools/lldb-instr/Inputs/foo.h
    lldb/trunk/lit/tools/lldb-instr/TestInstrumentationRecord.test
    lldb/trunk/lit/tools/lldb-instr/TestInstrumentationRegister.test
    lldb/trunk/tools/lldb-instr/Instrument.cpp

Modified: lldb/trunk/lit/tools/lldb-instr/Inputs/foo.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lit/tools/lldb-instr/Inputs/foo.cpp?rev=354301&r1=354300&r2=354301&view=diff
==============================================================================
--- lldb/trunk/lit/tools/lldb-instr/Inputs/foo.cpp (original)
+++ lldb/trunk/lit/tools/lldb-instr/Inputs/foo.cpp Mon Feb 18 17:04:31 2019
@@ -7,3 +7,5 @@ int Foo::C(int i) { return i; }
 int Foo::D(bool b) const { return 1; }
 void Foo::E() {}
 int Foo::F(int i) { return i; }
+void Foo::G(const char *fmt...) {}
+Foo Foo::H() { return Foo(); }

Modified: lldb/trunk/lit/tools/lldb-instr/Inputs/foo.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lit/tools/lldb-instr/Inputs/foo.h?rev=354301&r1=354300&r2=354301&view=diff
==============================================================================
--- lldb/trunk/lit/tools/lldb-instr/Inputs/foo.h (original)
+++ lldb/trunk/lit/tools/lldb-instr/Inputs/foo.h Mon Feb 18 17:04:31 2019
@@ -8,5 +8,6 @@ struct Foo {
   int D(bool b) const;
   static void E();
   static int F(int i);
-  int G() { return 0; }
+  void G(const char* fmt...);
+  static Foo H();
 };

Modified: lldb/trunk/lit/tools/lldb-instr/TestInstrumentationRecord.test
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lit/tools/lldb-instr/TestInstrumentationRecord.test?rev=354301&r1=354300&r2=354301&view=diff
==============================================================================
--- lldb/trunk/lit/tools/lldb-instr/TestInstrumentationRecord.test (original)
+++ lldb/trunk/lit/tools/lldb-instr/TestInstrumentationRecord.test Mon Feb 18 17:04:31 2019
@@ -12,3 +12,6 @@
 # CHECK: LLDB_RECORD_METHOD_CONST(int, Foo, D, (bool), b);
 # CHECK: LLDB_RECORD_STATIC_METHOD_NO_ARGS(void, Foo, E);
 # CHECK: LLDB_RECORD_STATIC_METHOD(int, Foo, F, (int), i);
+# CHECK-NOT: LLDB_RECORD_STATIC_METHOD(void, Foo, G
+# CHECK: LLDB_RECORD_STATIC_METHOD_NO_ARGS(Foo, Foo, H);
+# CHECK: LLDB_RECORD_RESULT(Foo())

Modified: lldb/trunk/lit/tools/lldb-instr/TestInstrumentationRegister.test
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lit/tools/lldb-instr/TestInstrumentationRegister.test?rev=354301&r1=354300&r2=354301&view=diff
==============================================================================
--- lldb/trunk/lit/tools/lldb-instr/TestInstrumentationRegister.test (original)
+++ lldb/trunk/lit/tools/lldb-instr/TestInstrumentationRegister.test Mon Feb 18 17:04:31 2019
@@ -10,3 +10,4 @@
 # CHECK: LLDB_REGISTER_METHOD_CONST(int, Foo, D, (bool));
 # CHECK: LLDB_REGISTER_STATIC_METHOD(void, Foo, E, ());
 # CHECK: LLDB_REGISTER_STATIC_METHOD(int, Foo, F, (int));
+# CHECK-NOT: LLDB_REGISTER_STATIC_METHOD(void, Foo, G

Modified: lldb/trunk/tools/lldb-instr/Instrument.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/lldb-instr/Instrument.cpp?rev=354301&r1=354300&r2=354301&view=diff
==============================================================================
--- lldb/trunk/tools/lldb-instr/Instrument.cpp (original)
+++ lldb/trunk/tools/lldb-instr/Instrument.cpp Mon Feb 18 17:04:31 2019
@@ -120,6 +120,33 @@ static std::string GetRegisterMethodMacr
   return OS.str();
 }
 
+class SBReturnVisitor : public RecursiveASTVisitor<SBReturnVisitor> {
+public:
+  SBReturnVisitor(Rewriter &R) : MyRewriter(R) {}
+
+  bool VisitReturnStmt(ReturnStmt *Stmt) {
+    Expr *E = Stmt->getRetValue();
+
+    if (E->getBeginLoc().isMacroID())
+      return false;
+
+    SourceRange R(E->getBeginLoc(), E->getEndLoc());
+
+    StringRef WrittenExpr = Lexer::getSourceText(
+        CharSourceRange::getTokenRange(R), MyRewriter.getSourceMgr(),
+        MyRewriter.getLangOpts());
+
+    std::string ReplacementText =
+        "LLDB_RECORD_RESULT(" + WrittenExpr.str() + ")";
+    MyRewriter.ReplaceText(R, ReplacementText);
+
+    return true;
+  }
+
+private:
+  Rewriter &MyRewriter;
+};
+
 class SBVisitor : public RecursiveASTVisitor<SBVisitor> {
 public:
   SBVisitor(Rewriter &R, ASTContext &Context)
@@ -200,6 +227,13 @@ public:
         MyRewriter.getLangOpts());
     MyRewriter.InsertTextAfter(InsertLoc, Macro);
 
+    // If the function returns a class or struct, we need to wrap its return
+    // statement(s).
+    if (ReturnType->isStructureOrClassType()) {
+      SBReturnVisitor Visitor(MyRewriter);
+      Visitor.TraverseDecl(Decl);
+    }
+
     return true;
   }
 
@@ -210,8 +244,9 @@ private:
   ///  1. Decls outside the main source file,
   ///  2. Decls that are only present in the source file,
   ///  3. Decls that are not definitions,
-  ///  4. Non-public decls,
-  ///  5. Destructors.
+  ///  4. Non-public methods,
+  ///  5. Variadic methods.
+  ///  6. Destructors.
   bool ShouldSkip(CXXMethodDecl *Decl) {
     // Skip anything outside the main file.
     if (!MyRewriter.getSourceMgr().isInMainFile(Decl->getBeginLoc()))
@@ -228,11 +263,15 @@ private:
     if (!Body)
       return true;
 
-    // Skip non-public decls.
+    // Skip non-public methods.
     AccessSpecifier AS = Decl->getAccess();
     if (AS != AccessSpecifier::AS_public)
       return true;
 
+    // Skip variadic methods.
+    if (Decl->isVariadic())
+      return true;
+
     // Skip destructors.
     if (isa<CXXDestructorDecl>(Decl))
       return true;




More information about the lldb-commits mailing list