summaryrefslogtreecommitdiffstats
path: root/clang/unittests/Tooling/ToolingTest.cpp
diff options
context:
space:
mode:
authorManuel Klimek <klimek@google.com>2011-04-27 16:39:14 +0000
committerManuel Klimek <klimek@google.com>2011-04-27 16:39:14 +0000
commit6825eebcd6e420e6d6415efefd6bfe75836ebe1e (patch)
tree5df643a09ca35c4d6ef0051d7ad0fb13f270c2b2 /clang/unittests/Tooling/ToolingTest.cpp
parent27c0c9bb87cd535c684b00c1617e344067351dd2 (diff)
downloadbcm5719-llvm-6825eebcd6e420e6d6415efefd6bfe75836ebe1e.tar.gz
bcm5719-llvm-6825eebcd6e420e6d6415efefd6bfe75836ebe1e.zip
This is the next step in building the standalone tools infrastructure:
This patch simplifies writing of standalone Clang tools. As an example, we add clang-check, a tool that runs a syntax only frontend action over a .cc file. When you integrate this into your favorite editor, you get much faster feedback on your compilation errors, thus reducing your feedback cycle especially when writing new code. The tool depends on integration of an outstanding patch to CMake to work which allows you to always have a current compile command database in your cmake output directory when you set CMAKE_EXPORT_COMPILE_COMMANDS. llvm-svn: 130306
Diffstat (limited to 'clang/unittests/Tooling/ToolingTest.cpp')
-rw-r--r--clang/unittests/Tooling/ToolingTest.cpp84
1 files changed, 84 insertions, 0 deletions
diff --git a/clang/unittests/Tooling/ToolingTest.cpp b/clang/unittests/Tooling/ToolingTest.cpp
index da89c0ba104..6e5bc6b613e 100644
--- a/clang/unittests/Tooling/ToolingTest.cpp
+++ b/clang/unittests/Tooling/ToolingTest.cpp
@@ -7,6 +7,7 @@
//
//===----------------------------------------------------------------------===//
+#include "llvm/ADT/Twine.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclGroup.h"
@@ -86,6 +87,89 @@ TEST(RunSyntaxOnlyToolOnCode, FindsClassDecl) {
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