[SCCP] Avoid modifying AdditionalUsers while iterating over it
[llvm-project.git] / clang / unittests / Analysis / CFGBuildResult.h
blob4851d3d7fb6d653316220f0196ad3989ace8be2f
1 //===- unittests/Analysis/CFGBuildResult.h - CFG tests --------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "clang/ASTMatchers/ASTMatchFinder.h"
10 #include "clang/Analysis/CFG.h"
11 #include "clang/Tooling/Tooling.h"
12 #include <memory>
14 namespace clang {
15 namespace analysis {
17 class BuildResult {
18 public:
19 enum Status {
20 ToolFailed,
21 ToolRan,
22 SawFunctionBody,
23 BuiltCFG,
26 BuildResult(Status S, const FunctionDecl *Func = nullptr,
27 std::unique_ptr<CFG> Cfg = nullptr,
28 std::unique_ptr<ASTUnit> AST = nullptr)
29 : S(S), Cfg(std::move(Cfg)), AST(std::move(AST)), Func(Func) {}
31 Status getStatus() const { return S; }
32 CFG *getCFG() const { return Cfg.get(); }
33 ASTUnit *getAST() const { return AST.get(); }
34 const FunctionDecl *getFunc() const { return Func; }
36 private:
37 Status S;
38 std::unique_ptr<CFG> Cfg;
39 std::unique_ptr<ASTUnit> AST;
40 const FunctionDecl *Func;
43 class CFGCallback : public ast_matchers::MatchFinder::MatchCallback {
44 public:
45 CFGCallback(std::unique_ptr<ASTUnit> AST) : AST(std::move(AST)) {}
47 std::unique_ptr<ASTUnit> AST;
48 BuildResult TheBuildResult = BuildResult::ToolRan;
49 CFG::BuildOptions Options;
51 void run(const ast_matchers::MatchFinder::MatchResult &Result) override {
52 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
53 Stmt *Body = Func->getBody();
54 if (!Body)
55 return;
56 TheBuildResult = BuildResult::SawFunctionBody;
57 Options.AddImplicitDtors = true;
58 if (std::unique_ptr<CFG> Cfg =
59 CFG::buildCFG(nullptr, Body, Result.Context, Options))
60 TheBuildResult = {BuildResult::BuiltCFG, Func, std::move(Cfg),
61 std::move(AST)};
65 inline BuildResult BuildCFG(const char *Code, CFG::BuildOptions Options = {}) {
66 std::vector<std::string> Args = {"-std=c++11",
67 "-fno-delayed-template-parsing"};
68 std::unique_ptr<ASTUnit> AST = tooling::buildASTFromCodeWithArgs(Code, Args);
69 if (!AST)
70 return BuildResult::ToolFailed;
72 CFGCallback Callback(std::move(AST));
73 Callback.Options = Options;
74 ast_matchers::MatchFinder Finder;
75 Finder.addMatcher(ast_matchers::functionDecl().bind("func"), &Callback);
77 Finder.matchAST(Callback.AST->getASTContext());
78 return std::move(Callback.TheBuildResult);
81 } // namespace analysis
82 } // namespace clang