1 //===- unittests/Analysis/CFGTest.cpp - CFG tests -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/ASTMatchers/ASTMatchFinder.h"
11 #include "clang/Analysis/CFG.h"
12 #include "clang/Tooling/Tooling.h"
13 #include "gtest/gtest.h"
28 class CFGCallback : public ast_matchers::MatchFinder::MatchCallback {
30 BuildResult TheBuildResult = ToolRan;
32 void run(const ast_matchers::MatchFinder::MatchResult &Result) override {
33 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
34 Stmt *Body = Func->getBody();
37 TheBuildResult = SawFunctionBody;
38 CFG::BuildOptions Options;
39 Options.AddImplicitDtors = true;
40 if (CFG::buildCFG(nullptr, Body, Result.Context, Options))
41 TheBuildResult = BuiltCFG;
45 BuildResult BuildCFG(const char *Code) {
48 ast_matchers::MatchFinder Finder;
49 Finder.addMatcher(ast_matchers::functionDecl().bind("func"), &Callback);
50 std::unique_ptr<tooling::FrontendActionFactory> Factory(
51 tooling::newFrontendActionFactory(&Finder));
52 std::vector<std::string> Args = {"-std=c++11", "-fno-delayed-template-parsing"};
53 if (!tooling::runToolOnCodeWithArgs(Factory->create(), Code, Args))
55 return Callback.TheBuildResult;
58 // Constructing a CFG for a range-based for over a dependent type fails (but
60 TEST(CFG, RangeBasedForOverDependentType) {
61 const char *Code = "class Foo;\n"
62 "template <typename T>\n"
63 "void f(const T &Range) {\n"
64 " for (const Foo *TheFoo : Range) {\n"
67 EXPECT_EQ(SawFunctionBody, BuildCFG(Code));
70 // Constructing a CFG containing a delete expression on a dependent type should
72 TEST(CFG, DeleteExpressionOnDependentType) {
73 const char *Code = "template<class T>\n"
77 EXPECT_EQ(BuiltCFG, BuildCFG(Code));
80 // Constructing a CFG on a function template with a variable of incomplete type
82 TEST(CFG, VariableOfIncompleteType) {
83 const char *Code = "template<class T> void f() {\n"
87 EXPECT_EQ(BuiltCFG, BuildCFG(Code));
91 } // namespace analysis