From cfe-commits Tue Nov 27 12:30:04 2012 From: Olivier Goffart Date: Tue, 27 Nov 2012 12:30:04 +0000 To: cfe-commits Subject: Re: [cfe-commits] [cfe-dev] Allow to selectively skip function bodies while parsing. Message-Id: <4185807.9TFm2EYv9W () gargamel> X-MARC-Message: https://marc.info/?l=cfe-commits&m=135407380626941 MIME-Version: 1 Content-Type: multipart/mixed; boundary="--nextPart1824210.QSYp5qnMaO" This is a multi-part message in MIME format. --nextPart1824210.QSYp5qnMaO Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" On Monday 26 November 2012 20:54:28 Richard Smith wrote: > On Mon, Nov 26, 2012 at 4:03 AM, Olivier Goffart 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 --nextPart1824210.QSYp5qnMaO Content-Disposition: attachment; filename="0001-Add-a-hook-in-the-ASTConsumer-to-be-able-to-skip-fun.patch" Content-Transfer-Encoding: 7Bit Content-Type: text/x-patch; charset="UTF-8"; name="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 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(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(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 --nextPart1824210.QSYp5qnMaO Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ cfe-commits mailing list cfe-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits --nextPart1824210.QSYp5qnMaO--