1 //===- unittests/Analysis/CFGTest.cpp - CFG tests -------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "clang/ASTMatchers/ASTMatchFinder.h"
11 #include "clang/Analysis/CFG.h"
12 #include "clang/Tooling/Tooling.h"
13 #include "gtest/gtest.h"
14 #include <string>
15 #include <vector>
16
17 namespace clang {
18 namespace analysis {
19 namespace {
20
21 // Constructing a CFG for a range-based for over a dependent type fails (but
22 // should not crash).
TEST(CFG,RangeBasedForOverDependentType)23 TEST(CFG, RangeBasedForOverDependentType) {
24 const char *Code = "class Foo;\n"
25 "template <typename T>\n"
26 "void f(const T &Range) {\n"
27 " for (const Foo *TheFoo : Range) {\n"
28 " }\n"
29 "}\n";
30
31 class CFGCallback : public ast_matchers::MatchFinder::MatchCallback {
32 public:
33 bool SawFunctionBody = false;
34
35 void run(const ast_matchers::MatchFinder::MatchResult &Result) override {
36 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
37 Stmt *Body = Func->getBody();
38 if (!Body)
39 return;
40 SawFunctionBody = true;
41 std::unique_ptr<CFG> cfg =
42 CFG::buildCFG(nullptr, Body, Result.Context, CFG::BuildOptions());
43 EXPECT_EQ(nullptr, cfg);
44 }
45 } Callback;
46
47 ast_matchers::MatchFinder Finder;
48 Finder.addMatcher(ast_matchers::functionDecl().bind("func"), &Callback);
49 std::unique_ptr<tooling::FrontendActionFactory> Factory(
50 tooling::newFrontendActionFactory(&Finder));
51 std::vector<std::string> Args = {"-std=c++11", "-fno-delayed-template-parsing"};
52 ASSERT_TRUE(tooling::runToolOnCodeWithArgs(Factory->create(), Code, Args));
53 EXPECT_TRUE(Callback.SawFunctionBody);
54 }
55
56 } // namespace
57 } // namespace analysis
58 } // namespace clang
59