blob: e691110050a22dbefdbb1ff2ef299459e1f44cb1 [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
Martin Bohmef44cde82016-12-05 11:33:19 +000021enum BuildResult {
22 ToolFailed,
23 ToolRan,
24 SawFunctionBody,
25 BuiltCFG,
26};
27
28class CFGCallback : public ast_matchers::MatchFinder::MatchCallback {
29public:
30 BuildResult TheBuildResult = ToolRan;
31
32 void run(const ast_matchers::MatchFinder::MatchResult &Result) override {
33 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
34 Stmt *Body = Func->getBody();
35 if (!Body)
36 return;
37 TheBuildResult = SawFunctionBody;
38 if (CFG::buildCFG(nullptr, Body, Result.Context, CFG::BuildOptions()))
39 TheBuildResult = BuiltCFG;
40 }
41};
42
43BuildResult BuildCFG(const char *Code) {
44 CFGCallback Callback;
45
46 ast_matchers::MatchFinder Finder;
47 Finder.addMatcher(ast_matchers::functionDecl().bind("func"), &Callback);
48 std::unique_ptr<tooling::FrontendActionFactory> Factory(
49 tooling::newFrontendActionFactory(&Finder));
50 std::vector<std::string> Args = {"-std=c++11", "-fno-delayed-template-parsing"};
51 if (!tooling::runToolOnCodeWithArgs(Factory->create(), Code, Args))
52 return ToolFailed;
53 return Callback.TheBuildResult;
54}
55
Alexander Kornienkoff2046a2016-07-08 10:50:51 +000056// Constructing a CFG for a range-based for over a dependent type fails (but
57// should not crash).
58TEST(CFG, RangeBasedForOverDependentType) {
59 const char *Code = "class Foo;\n"
60 "template <typename T>\n"
61 "void f(const T &Range) {\n"
62 " for (const Foo *TheFoo : Range) {\n"
63 " }\n"
64 "}\n";
Martin Bohmef44cde82016-12-05 11:33:19 +000065 EXPECT_EQ(SawFunctionBody, BuildCFG(Code));
66}
Alexander Kornienkoff2046a2016-07-08 10:50:51 +000067
Martin Bohmef44cde82016-12-05 11:33:19 +000068// Constructing a CFG containing a delete expression on a dependent type should
69// not crash.
70TEST(CFG, DeleteExpressionOnDependentType) {
71 const char *Code = "template<class T>\n"
72 "void f(T t) {\n"
73 " delete t;\n"
74 "}\n";
75 EXPECT_EQ(BuiltCFG, BuildCFG(Code));
Alexander Kornienkoff2046a2016-07-08 10:50:51 +000076}
77
78} // namespace
79} // namespace analysis
80} // namespace clang