blob: 768705f46f29f7e55fadd93574dc4c2eff9492a2 [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;
Martin Bohme0c11c292017-03-07 08:42:37 +000038 CFG::BuildOptions Options;
39 Options.AddImplicitDtors = true;
40 if (CFG::buildCFG(nullptr, Body, Result.Context, Options))
Martin Bohmef44cde82016-12-05 11:33:19 +000041 TheBuildResult = BuiltCFG;
42 }
43};
44
45BuildResult BuildCFG(const char *Code) {
46 CFGCallback Callback;
47
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))
54 return ToolFailed;
55 return Callback.TheBuildResult;
56}
57
Alexander Kornienkoff2046a2016-07-08 10:50:51 +000058// Constructing a CFG for a range-based for over a dependent type fails (but
59// should not crash).
60TEST(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"
65 " }\n"
66 "}\n";
Martin Bohmef44cde82016-12-05 11:33:19 +000067 EXPECT_EQ(SawFunctionBody, BuildCFG(Code));
68}
Alexander Kornienkoff2046a2016-07-08 10:50:51 +000069
Martin Bohmef44cde82016-12-05 11:33:19 +000070// Constructing a CFG containing a delete expression on a dependent type should
71// not crash.
72TEST(CFG, DeleteExpressionOnDependentType) {
73 const char *Code = "template<class T>\n"
74 "void f(T t) {\n"
75 " delete t;\n"
76 "}\n";
77 EXPECT_EQ(BuiltCFG, BuildCFG(Code));
Alexander Kornienkoff2046a2016-07-08 10:50:51 +000078}
79
Martin Bohme0c11c292017-03-07 08:42:37 +000080// Constructing a CFG on a function template with a variable of incomplete type
81// should not crash.
82TEST(CFG, VariableOfIncompleteType) {
83 const char *Code = "template<class T> void f() {\n"
84 " class Undefined;\n"
85 " Undefined u;\n"
86 "}\n";
87 EXPECT_EQ(BuiltCFG, BuildCFG(Code));
88}
89
Alexander Kornienkoff2046a2016-07-08 10:50:51 +000090} // namespace
91} // namespace analysis
92} // namespace clang