Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit d1bac8d

Browse files
committed
Allow an ASTConsumer to selectively skip function bodies while parsing. Patch
by Olivier Goffart! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@168726 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent d0adeb6 commit d1bac8d

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

Diff for: include/clang/AST/ASTConsumer.h

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace clang {
1818
class ASTContext;
1919
class CXXRecordDecl;
20+
class Decl;
2021
class DeclGroupRef;
2122
class HandleTagDeclDefinition;
2223
class PPMutationListener;
@@ -130,6 +131,14 @@ class ASTConsumer {
130131

131132
/// PrintStats - If desired, print any statistics.
132133
virtual void PrintStats() {}
134+
135+
/// \brief This callback is called for each function if the Parser was
136+
/// initialized with \c SkipFunctionBodies set to \c true.
137+
///
138+
/// \return \c true if the function's body should be skipped. The function
139+
/// body may be parsed anyway if it is needed (for instance, if it contains
140+
/// the code completion point or is constexpr).
141+
virtual bool shouldSkipFunctionBody(Decl *D) { return true; }
133142
};
134143

135144
} // end namespace clang.

Diff for: lib/Sema/SemaDecl.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -7993,6 +7993,9 @@ void Sema::computeNRVO(Stmt *Body, FunctionScopeInfo *Scope) {
79937993
}
79947994

79957995
bool Sema::canSkipFunctionBody(Decl *D) {
7996+
if (!Consumer.shouldSkipFunctionBody(D))
7997+
return false;
7998+
79967999
if (isa<ObjCMethodDecl>(D))
79978000
return true;
79988001

Diff for: unittests/Tooling/ToolingTest.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "clang/AST/ASTConsumer.h"
1111
#include "clang/AST/DeclCXX.h"
1212
#include "clang/AST/DeclGroup.h"
13+
#include "clang/Frontend/CompilerInstance.h"
1314
#include "clang/Frontend/FrontendAction.h"
1415
#include "clang/Frontend/FrontendActions.h"
1516
#include "clang/Tooling/CompilationDatabase.h"
@@ -162,5 +163,28 @@ TEST(newFrontendActionFactory, InjectsEndOfSourceFileCallback) {
162163
}
163164
#endif
164165

166+
struct SkipBodyConsumer : public clang::ASTConsumer {
167+
/// Skip the 'skipMe' function.
168+
virtual bool shouldSkipFunctionBody(Decl *D) {
169+
FunctionDecl *F = dyn_cast<FunctionDecl>(D);
170+
return F && F->getNameAsString() == "skipMe";
171+
}
172+
};
173+
174+
struct SkipBodyAction : public clang::ASTFrontendAction {
175+
virtual ASTConsumer *CreateASTConsumer(CompilerInstance &Compiler,
176+
StringRef) {
177+
Compiler.getFrontendOpts().SkipFunctionBodies = true;
178+
return new SkipBodyConsumer;
179+
}
180+
};
181+
182+
TEST(runToolOnCode, TestSkipFunctionBoddy) {
183+
EXPECT_TRUE(runToolOnCode(new SkipBodyAction,
184+
"int skipMe() { an_error_here }"));
185+
EXPECT_FALSE(runToolOnCode(new SkipBodyAction,
186+
"int skipMeNot() { an_error_here }"));
187+
}
188+
165189
} // end namespace tooling
166190
} // end namespace clang

0 commit comments

Comments
 (0)