[prev in list] [next in list] [prev in thread] [next in thread] 

List:       cfe-commits
Subject:    Re: [cfe-commits] [cfe-dev] Allow to selectively skip function bodies while parsing.
From:       Olivier Goffart <ogoffart () kde ! org>
Date:       2012-11-27 12:30:04
Message-ID: 4185807.9TFm2EYv9W () gargamel
[Download RAW message or body]

On Monday 26 November 2012 20:54:28 Richard Smith wrote:
> On Mon, Nov 26, 2012 at 4:03 AM, Olivier Goffart <ogoffart@kde.org> wrote:
> > Hi,
> > 
> > I would like to upstream the attached patch which allows ASTConsumer to
> > select
> > which function to skip while parsing.
> > 
> > I have been using it to do a online code browser:  http://code.woboq.org
> > 
> > Most of the time of my tool is spent in parsing.
> > And I have to parse the same includes again and again, and some includes
> > have
> > a lot of inline functions.
> > By skipping body in already seen includes, I was able to reduce the
> > parsing
> > time by 30%.
> > 
> > This might also be useful for other tools like refactoring or so.
> 
> Thanks, the patch looks good. Please add a test;
> unittests/Tooling/ToolingTest.cpp would be a reasonable fit. You could test
> this by using a test case containing a function with an error in its body,
> and ensure that your hook allows you to control when the parser hits the
> error.

Hi,

Same patch with a test.

Regards
-- 
Olivier
["0001-Add-a-hook-in-the-ASTConsumer-to-be-able-to-skip-fun.patch" (0001-Add-a-hook-in-the-ASTConsumer-to-be-able-to-skip-fun.patch)]

From 9bfd913543560498231e4af51ad8c4e337fe380b Mon Sep 17 00:00:00 2001
From: Olivier Goffart <ogoffart@woboq.com>
Date: Sat, 1 Sep 2012 18:17:14 +0200
Subject: [PATCH] Add a hook in the ASTConsumer to be able to skip function
 body

---
 include/clang/AST/ASTConsumer.h   |  5 +++++
 lib/Sema/SemaDecl.cpp             |  3 +++
 unittests/Tooling/ToolingTest.cpp | 24 ++++++++++++++++++++++++
 3 files changed, 32 insertions(+)

diff --git a/include/clang/AST/ASTConsumer.h b/include/clang/AST/ASTConsumer.h
index 37b0740..c366024 100644
--- a/include/clang/AST/ASTConsumer.h
+++ b/include/clang/AST/ASTConsumer.h
@@ -27,6 +27,7 @@ namespace clang {
   class VarDecl;
   class FunctionDecl;
   class ImportDecl;
+  class Decl;
 
 /// ASTConsumer - This is an abstract interface that should be implemented by
 /// clients that read ASTs.  This abstraction layer allows the client to be
@@ -130,6 +131,10 @@ public:
 
   /// PrintStats - If desired, print any statistics.
   virtual void PrintStats() {}
+
+  /// This callback is called for each function if the Parser was initialized
+  /// with SkipFunctionBodies.  Returns true if the function should be skipped
+  virtual bool shouldSkipFunctionBody(Decl *D) { return true; }
 };
 
 } // end namespace clang.
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index 629fccc..554eb24 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -7985,6 +7985,9 @@ void Sema::computeNRVO(Stmt *Body, FunctionScopeInfo *Scope) {
 }
 
 bool Sema::canSkipFunctionBody(Decl *D) {
+  if (!Consumer.shouldSkipFunctionBody(D))
+    return false;
+
   if (isa<ObjCMethodDecl>(D))
     return true;
 
diff --git a/unittests/Tooling/ToolingTest.cpp b/unittests/Tooling/ToolingTest.cpp
index d40c613..d32e049 100644
--- a/unittests/Tooling/ToolingTest.cpp
+++ b/unittests/Tooling/ToolingTest.cpp
@@ -12,6 +12,7 @@
 #include "clang/AST/DeclGroup.h"
 #include "clang/Frontend/FrontendAction.h"
 #include "clang/Frontend/FrontendActions.h"
+#include "clang/Frontend/CompilerInstance.h"
 #include "clang/Tooling/CompilationDatabase.h"
 #include "clang/Tooling/Tooling.h"
 #include "gtest/gtest.h"
@@ -162,5 +163,28 @@ TEST(newFrontendActionFactory, InjectsEndOfSourceFileCallback) {
 }
 #endif
 
+struct SkipBodyConsumer : public clang::ASTConsumer {
+  // skip the 'skipMe' function
+  virtual bool shouldSkipFunctionBody(Decl* D) {
+    FunctionDecl* F = dyn_cast<FunctionDecl>(D);
+    if (!F)
+      return false;
+    return F->getNameAsString() == "skipMe";
+  }
+};
+
+struct SkipBodyAction : public clang::ASTFrontendAction {
+  virtual clang::ASTConsumer* CreateASTConsumer(
+      clang::CompilerInstance& compiler, StringRef dummy) {
+    compiler.getFrontendOpts().SkipFunctionBodies = true;
+    return new SkipBodyConsumer;
+  }
+};
+
+TEST(runToolOnCode, TestSkipFunctionBoddy) {
+  EXPECT_TRUE(runToolOnCode(new SkipBodyAction, "int skipMe() { an_error_here }"));
+  EXPECT_FALSE(runToolOnCode(new SkipBodyAction, "int skipMeNot() { an_error_here }"));
+}
+
 } // end namespace tooling
 } // end namespace clang
-- 
1.7.12.1



_______________________________________________
cfe-commits mailing list
cfe-commits@cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic