Skip to content

Commit 246626a

Browse files
committed
[clang][dataflow] Add a ControlFlowContext::build() overload taking a FunctionDecl.
This is the most common use case, so it makes sense to have a specific overload for it. Reviewed By: xazax.hun Differential Revision: https://reviews.llvm.org/D151183
1 parent ca7167d commit 246626a

File tree

5 files changed

+20
-6
lines changed

5 files changed

+20
-6
lines changed

clang/include/clang/Analysis/FlowSensitive/ControlFlowContext.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ namespace dataflow {
3131
/// analysis.
3232
class ControlFlowContext {
3333
public:
34+
/// Builds a ControlFlowContext from a `FunctionDecl`.
35+
/// `Func.hasBody()` must be true, and `Func.isTemplated()` must be false.
36+
static llvm::Expected<ControlFlowContext> build(const FunctionDecl &Func,
37+
ASTContext &C);
38+
3439
/// Builds a ControlFlowContext from an AST node. `D` is the function in which
3540
/// `S` resides. `D.isTemplated()` must be false.
3641
static llvm::Expected<ControlFlowContext> build(const Decl &D, Stmt &S,

clang/lib/Analysis/FlowSensitive/ControlFlowContext.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ static llvm::BitVector findReachableBlocks(const CFG &Cfg) {
6767
return BlockReachable;
6868
}
6969

70+
llvm::Expected<ControlFlowContext>
71+
ControlFlowContext::build(const FunctionDecl &Func, ASTContext &C) {
72+
if (!Func.hasBody())
73+
return llvm::createStringError(
74+
std::make_error_code(std::errc::invalid_argument),
75+
"Cannot analyze function without a body");
76+
77+
return build(Func, *Func.getBody(), C);
78+
}
79+
7080
llvm::Expected<ControlFlowContext>
7181
ControlFlowContext::build(const Decl &D, Stmt &S, ASTContext &C) {
7282
if (D.isTemplated())

clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ DataflowAnalysisContext::getControlFlowContext(const FunctionDecl *F) {
210210
if (It != FunctionContexts.end())
211211
return &It->second;
212212

213-
if (Stmt *Body = F->getBody()) {
214-
auto CFCtx = ControlFlowContext::build(*F, *Body, F->getASTContext());
213+
if (F->hasBody()) {
214+
auto CFCtx = ControlFlowContext::build(*F, F->getASTContext());
215215
// FIXME: Handle errors.
216216
assert(CFCtx);
217217
auto Result = FunctionContexts.insert({F, std::move(*CFCtx)});

clang/unittests/Analysis/FlowSensitive/TestingSupport.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,7 @@ checkDataflow(AnalysisInputs<AnalysisT> AI,
241241
llvm::errc::invalid_argument, "Could not find the target function.");
242242

243243
// Build the control flow graph for the target function.
244-
auto MaybeCFCtx =
245-
ControlFlowContext::build(*Target, *Target->getBody(), Context);
244+
auto MaybeCFCtx = ControlFlowContext::build(*Target, Context);
246245
if (!MaybeCFCtx) return MaybeCFCtx.takeError();
247246
auto &CFCtx = *MaybeCFCtx;
248247

clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ runAnalysis(llvm::StringRef Code, AnalysisT (*MakeAnalysis)(ASTContext &)) {
6767
Stmt *Body = Func->getBody();
6868
assert(Body != nullptr);
6969

70-
auto CFCtx = llvm::cantFail(
71-
ControlFlowContext::build(*Func, *Body, AST->getASTContext()));
70+
auto CFCtx =
71+
llvm::cantFail(ControlFlowContext::build(*Func, AST->getASTContext()));
7272

7373
AnalysisT Analysis = MakeAnalysis(AST->getASTContext());
7474
DataflowAnalysisContext DACtx(std::make_unique<WatchedLiteralsSolver>());

0 commit comments

Comments
 (0)