blob: 715fdcb80605698168f194a84315a325c3971e30 [file] [log] [blame]
Alexander Kornienkoff2046a2016-07-08 10:50:51 +00001//===- 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
17namespace clang {
18namespace analysis {
19namespace {
20
21// Constructing a CFG for a range-based for over a dependent type fails (but
22// should not crash).
23TEST(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"};
52 ASSERT_TRUE(tooling::runToolOnCodeWithArgs(Factory->create(), Code, Args));
53 EXPECT_TRUE(Callback.SawFunctionBody);
54}
55
56} // namespace
57} // namespace analysis
58} // namespace clang