diff options
author | Manuel Klimek <klimek@google.com> | 2011-04-27 16:39:14 +0000 |
---|---|---|
committer | Manuel Klimek <klimek@google.com> | 2011-04-27 16:39:14 +0000 |
commit | 9a05fa97df4d5e64f740848a7759ecae5e1ed162 (patch) | |
tree | 70f5f56fcbe1bd6d3ff66ee1af96fa793fc01923 /unittests/Tooling/ToolingTest.cpp | |
parent | 4d8d803b06804defe25346871c7beb6096540c4a (diff) |
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.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@130306 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/Tooling/ToolingTest.cpp')
-rw-r--r-- | unittests/Tooling/ToolingTest.cpp | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/unittests/Tooling/ToolingTest.cpp b/unittests/Tooling/ToolingTest.cpp index da89c0ba10..6e5bc6b613 100644 --- a/unittests/Tooling/ToolingTest.cpp +++ b/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 |