summaryrefslogtreecommitdiffstats
path: root/unittests/Tooling/ToolingTest.cpp
blob: 6e5bc6b613e01041ffb7f1a9da5526ceef2e5b38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//===- unittest/Tooling/ToolingTest.cpp - Tooling unit tests --------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/Twine.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclGroup.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Tooling/Tooling.h"
#include "gtest/gtest.h"

namespace clang {
namespace tooling {

namespace {
/// Takes an ast consumer and returns it from CreateASTConsumer. This only
/// works with single translation unit compilations.
class TestAction : public clang::ASTFrontendAction {
 public:
  /// Takes ownership of TestConsumer.
  explicit TestAction(clang::ASTConsumer *TestConsumer)
      : TestConsumer(TestConsumer) {}

 protected:
  virtual clang::ASTConsumer* CreateASTConsumer(
      clang::CompilerInstance& compiler, llvm::StringRef dummy) {
    /// TestConsumer will be deleted by the framework calling us.
    return TestConsumer;
  }

 private:
  clang::ASTConsumer * const TestConsumer;
};

class FindTopLevelDeclConsumer : public clang::ASTConsumer {
 public:
  explicit FindTopLevelDeclConsumer(bool *FoundTopLevelDecl)
      : FoundTopLevelDecl(FoundTopLevelDecl) {}
  virtual void HandleTopLevelDecl(clang::DeclGroupRef DeclGroup) {
    *FoundTopLevelDecl = true;
  }
 private:
  bool * const FoundTopLevelDecl;
};
} // end namespace

TEST(RunSyntaxOnlyToolOnCode, FindsTopLevelDeclOnEmptyCode) {
  bool FoundTopLevelDecl = false;
  EXPECT_TRUE(RunSyntaxOnlyToolOnCode(
      new TestAction(new FindTopLevelDeclConsumer(&FoundTopLevelDecl)), ""));
  EXPECT_TRUE(FoundTopLevelDecl);
}

namespace {
class FindClassDeclXConsumer : public clang::ASTConsumer {
 public:
  FindClassDeclXConsumer(bool *FoundClassDeclX)
      : FoundClassDeclX(FoundClassDeclX) {}
  virtual void HandleTopLevelDecl(clang::DeclGroupRef GroupRef) {
    if (CXXRecordDecl* Record = llvm::dyn_cast<clang::CXXRecordDecl>(
            *GroupRef.begin())) {
      if (Record->getName() == "X") {
        *FoundClassDeclX = true;
      }
    }
  }
 private:
  bool *FoundClassDeclX;
};
} // end namespace

TEST(RunSyntaxOnlyToolOnCode, FindsClassDecl) {
  bool FoundClassDeclX = false;
  EXPECT_TRUE(RunSyntaxOnlyToolOnCode(new TestAction(
      new FindClassDeclXConsumer(&FoundClassDeclX)), "class X;"));
  EXPECT_TRUE(FoundClassDeclX);

  FoundClassDeclX = false;
  EXPECT_TRUE(RunSyntaxOnlyToolOnCode(new TestAction(
      new FindClassDeclXConsumer(&FoundClassDeclX)), "class Y;"));
  EXPECT_FALSE(FoundClassDeclX);
}

TEST(FindCompileArgsInJsonDatabase, FindsNothingIfEmpty) {
  std::string ErrorMessage;
  CompileCommand NotFound = FindCompileArgsInJsonDatabase(
      "a-file.cpp", "", ErrorMessage);
  EXPECT_TRUE(NotFound.CommandLine.empty()) << ErrorMessage;
  EXPECT_TRUE(NotFound.Directory.empty()) << ErrorMessage;
}

TEST(FindCompileArgsInJsonDatabase, ReadsSingleEntry) {
  llvm::StringRef Directory("/some/directory");
  llvm::StringRef FileName("/path/to/a-file.cpp");
  llvm::StringRef Command("/path/to/compiler and some arguments");
  std::string ErrorMessage;
  CompileCommand FoundCommand = FindCompileArgsInJsonDatabase(
      FileName,
      (llvm::Twine("[{\"directory\":\"") + Directory + "\"," +
                     "\"command\":\"" + Command + "\","
                     "\"file\":\"" + FileName + "\"}]").str(), ErrorMessage);
  EXPECT_EQ(Directory, FoundCommand.Directory) << ErrorMessage;
  ASSERT_EQ(4u, FoundCommand.CommandLine.size()) << ErrorMessage;
  EXPECT_EQ("/path/to/compiler", FoundCommand.CommandLine[0]) << ErrorMessage;
  EXPECT_EQ("and", FoundCommand.CommandLine[1]) << ErrorMessage;
  EXPECT_EQ("some", FoundCommand.CommandLine[2]) << ErrorMessage;
  EXPECT_EQ("arguments", FoundCommand.CommandLine[3]) << ErrorMessage;

  CompileCommand NotFound = FindCompileArgsInJsonDatabase(
      "a-file.cpp",
      (llvm::Twine("[{\"directory\":\"") + Directory + "\"," +
                     "\"command\":\"" + Command + "\","
                     "\"file\":\"" + FileName + "\"}]").str(), ErrorMessage);
  EXPECT_TRUE(NotFound.Directory.empty()) << ErrorMessage;
  EXPECT_TRUE(NotFound.CommandLine.empty()) << ErrorMessage;
}

TEST(FindCompileArgsInJsonDatabase, ReadsCompileCommandLinesWithSpaces) {
  llvm::StringRef Directory("/some/directory");
  llvm::StringRef FileName("/path/to/a-file.cpp");
  llvm::StringRef Command("\\\"/path to compiler\\\" \\\"and an argument\\\"");
  std::string ErrorMessage;
  CompileCommand FoundCommand = FindCompileArgsInJsonDatabase(
      FileName,
      (llvm::Twine("[{\"directory\":\"") + Directory + "\"," +
                     "\"command\":\"" + Command + "\","
                     "\"file\":\"" + FileName + "\"}]").str(), ErrorMessage);
  ASSERT_EQ(2u, FoundCommand.CommandLine.size());
  EXPECT_EQ("/path to compiler", FoundCommand.CommandLine[0]) << ErrorMessage;
  EXPECT_EQ("and an argument", FoundCommand.CommandLine[1]) << ErrorMessage;
}

TEST(FindCompileArgsInJsonDatabase, ReadsDirectoryWithSpaces) {
  llvm::StringRef Directory("/some directory / with spaces");
  llvm::StringRef FileName("/path/to/a-file.cpp");
  llvm::StringRef Command("a command");
  std::string ErrorMessage;
  CompileCommand FoundCommand = FindCompileArgsInJsonDatabase(
      FileName,
      (llvm::Twine("[{\"directory\":\"") + Directory + "\"," +
                     "\"command\":\"" + Command + "\","
                     "\"file\":\"" + FileName + "\"}]").str(), ErrorMessage);
  EXPECT_EQ(Directory, FoundCommand.Directory) << ErrorMessage;
}

TEST(FindCompileArgsInJsonDatabase, FindsEntry) {
  llvm::StringRef Directory("directory");
  llvm::StringRef FileName("file");
  llvm::StringRef Command("command");
  std::string JsonDatabase = "[";
  for (int I = 0; I < 10; ++I) {
    if (I > 0) JsonDatabase += ",";
    JsonDatabase += (llvm::Twine(
        "{\"directory\":\"") + Directory + llvm::Twine(I) + "\"," +
         "\"command\":\"" + Command + llvm::Twine(I) + "\","
         "\"file\":\"" + FileName + llvm::Twine(I) + "\"}").str();
  }
  JsonDatabase += "]";
  std::string ErrorMessage;
  CompileCommand FoundCommand = FindCompileArgsInJsonDatabase(
      "file4", JsonDatabase, ErrorMessage);
  EXPECT_EQ("directory4", FoundCommand.Directory) << ErrorMessage;
  ASSERT_EQ(1u, FoundCommand.CommandLine.size()) << ErrorMessage;
  EXPECT_EQ("command4", FoundCommand.CommandLine[0]) << ErrorMessage;
}

} // end namespace tooling
} // end namespace clang

OpenPOWER on IntegriCloud