diff options
author | dim <dim@FreeBSD.org> | 2011-05-02 19:39:53 +0000 |
---|---|---|
committer | dim <dim@FreeBSD.org> | 2011-05-02 19:39:53 +0000 |
commit | 110eaaceddcec790f7e6a5e3bf1261c9aa1e73ab (patch) | |
tree | 64a10f4c4154739d4a8191d7e1b52ce497f4ebd6 /unittests | |
parent | a0fb00f9837bd0d2e5948f16f6a6b82a7a628f51 (diff) | |
download | FreeBSD-src-110eaaceddcec790f7e6a5e3bf1261c9aa1e73ab.zip FreeBSD-src-110eaaceddcec790f7e6a5e3bf1261c9aa1e73ab.tar.gz |
Vendor import of clang trunk r130700:
http://llvm.org/svn/llvm-project/cfe/trunk@130700
Diffstat (limited to 'unittests')
-rw-r--r-- | unittests/CMakeLists.txt | 10 | ||||
-rw-r--r-- | unittests/Tooling/JsonCompileCommandLineDatabaseTest.cpp | 232 | ||||
-rw-r--r-- | unittests/Tooling/ToolingTest.cpp | 175 |
3 files changed, 417 insertions, 0 deletions
diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt index 112d6a0..f7f495e 100644 --- a/unittests/CMakeLists.txt +++ b/unittests/CMakeLists.txt @@ -59,3 +59,13 @@ add_clang_unittest(Frontend Frontend/FrontendActionTest.cpp USED_LIBS gtest gtest_main clangFrontend ) + +add_clang_unittest(Tooling + Tooling/ToolingTest.cpp + USED_LIBS gtest gtest_main clangTooling + ) + +add_clang_unittest(JsonCompileCommandLineDatabase + Tooling/JsonCompileCommandLineDatabaseTest.cpp + USED_LIBS gtest gtest_main clangTooling + ) diff --git a/unittests/Tooling/JsonCompileCommandLineDatabaseTest.cpp b/unittests/Tooling/JsonCompileCommandLineDatabaseTest.cpp new file mode 100644 index 0000000..d875293 --- /dev/null +++ b/unittests/Tooling/JsonCompileCommandLineDatabaseTest.cpp @@ -0,0 +1,232 @@ +//===- unittest/Tooling/JsonCompileCommandLineDatabaseTest ----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "../../lib/Tooling/JsonCompileCommandLineDatabase.h" +#include "gtest/gtest.h" + +namespace clang { +namespace tooling { + +TEST(UnescapeJsonCommandLine, ReturnsEmptyArrayOnEmptyString) { + std::vector<std::string> Result = UnescapeJsonCommandLine(""); + EXPECT_TRUE(Result.empty()); +} + +TEST(UnescapeJsonCommandLine, SplitsOnSpaces) { + std::vector<std::string> Result = UnescapeJsonCommandLine("a b c"); + ASSERT_EQ(3ul, Result.size()); + EXPECT_EQ("a", Result[0]); + EXPECT_EQ("b", Result[1]); + EXPECT_EQ("c", Result[2]); +} + +TEST(UnescapeJsonCommandLine, MungesMultipleSpaces) { + std::vector<std::string> Result = UnescapeJsonCommandLine(" a b "); + ASSERT_EQ(2ul, Result.size()); + EXPECT_EQ("a", Result[0]); + EXPECT_EQ("b", Result[1]); +} + +TEST(UnescapeJsonCommandLine, UnescapesBackslashCharacters) { + std::vector<std::string> Backslash = UnescapeJsonCommandLine("a\\\\\\\\"); + ASSERT_EQ(1ul, Backslash.size()); + EXPECT_EQ("a\\", Backslash[0]); + std::vector<std::string> Quote = UnescapeJsonCommandLine("a\\\\\\\""); + ASSERT_EQ(1ul, Quote.size()); + EXPECT_EQ("a\"", Quote[0]); +} + +TEST(UnescapeJsonCommandLine, DoesNotMungeSpacesBetweenQuotes) { + std::vector<std::string> Result = UnescapeJsonCommandLine("\\\" a b \\\""); + ASSERT_EQ(1ul, Result.size()); + EXPECT_EQ(" a b ", Result[0]); +} + +TEST(UnescapeJsonCommandLine, AllowsMultipleQuotedArguments) { + std::vector<std::string> Result = UnescapeJsonCommandLine( + " \\\" a \\\" \\\" b \\\" "); + ASSERT_EQ(2ul, Result.size()); + EXPECT_EQ(" a ", Result[0]); + EXPECT_EQ(" b ", Result[1]); +} + +TEST(UnescapeJsonCommandLine, AllowsEmptyArgumentsInQuotes) { + std::vector<std::string> Result = UnescapeJsonCommandLine( + "\\\"\\\"\\\"\\\""); + ASSERT_EQ(1ul, Result.size()); + EXPECT_TRUE(Result[0].empty()) << Result[0]; +} + +TEST(UnescapeJsonCommandLine, ParsesEscapedQuotesInQuotedStrings) { + std::vector<std::string> Result = UnescapeJsonCommandLine( + "\\\"\\\\\\\"\\\""); + ASSERT_EQ(1ul, Result.size()); + EXPECT_EQ("\"", Result[0]); +} + +TEST(UnescapeJsonCommandLine, ParsesMultipleArgumentsWithEscapedCharacters) { + std::vector<std::string> Result = UnescapeJsonCommandLine( + " \\\\\\\" \\\"a \\\\\\\" b \\\" \\\"and\\\\\\\\c\\\" \\\\\\\""); + ASSERT_EQ(4ul, Result.size()); + EXPECT_EQ("\"", Result[0]); + EXPECT_EQ("a \" b ", Result[1]); + EXPECT_EQ("and\\c", Result[2]); + EXPECT_EQ("\"", Result[3]); +} + +TEST(UnescapeJsonCommandLine, ParsesStringsWithoutSpacesIntoSingleArgument) { + std::vector<std::string> QuotedNoSpaces = UnescapeJsonCommandLine( + "\\\"a\\\"\\\"b\\\""); + ASSERT_EQ(1ul, QuotedNoSpaces.size()); + EXPECT_EQ("ab", QuotedNoSpaces[0]); + + std::vector<std::string> MixedNoSpaces = UnescapeJsonCommandLine( + "\\\"a\\\"bcd\\\"ef\\\"\\\"\\\"\\\"g\\\""); + ASSERT_EQ(1ul, MixedNoSpaces.size()); + EXPECT_EQ("abcdefg", MixedNoSpaces[0]); +} + +TEST(JsonCompileCommandLineParser, FailsOnEmptyString) { + JsonCompileCommandLineParser Parser("", NULL); + EXPECT_FALSE(Parser.Parse()) << Parser.GetErrorMessage(); +} + +TEST(JsonCompileCommandLineParser, DoesNotReadAfterInput) { + JsonCompileCommandLineParser Parser(llvm::StringRef(NULL, 0), NULL); + EXPECT_FALSE(Parser.Parse()) << Parser.GetErrorMessage(); +} + +TEST(JsonCompileCommandLineParser, ParsesEmptyArray) { + JsonCompileCommandLineParser Parser("[]", NULL); + EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage(); +} + +TEST(JsonCompileCommandLineParser, FailsIfNotClosingArray) { + JsonCompileCommandLineParser JustOpening("[", NULL); + EXPECT_FALSE(JustOpening.Parse()) << JustOpening.GetErrorMessage(); + JsonCompileCommandLineParser WithSpaces(" [ ", NULL); + EXPECT_FALSE(WithSpaces.Parse()) << WithSpaces.GetErrorMessage(); + JsonCompileCommandLineParser WithGarbage(" [x", NULL); + EXPECT_FALSE(WithGarbage.Parse()) << WithGarbage.GetErrorMessage(); +} + +TEST(JsonCompileCommandLineParser, ParsesEmptyArrayWithWhitespace) { + JsonCompileCommandLineParser Spaces(" [ ] ", NULL); + EXPECT_TRUE(Spaces.Parse()) << Spaces.GetErrorMessage(); + JsonCompileCommandLineParser AllWhites("\t\r\n[\t\n \t\r ]\t\r \n\n", NULL); + EXPECT_TRUE(AllWhites.Parse()) << AllWhites.GetErrorMessage(); +} + +TEST(JsonCompileCommandLineParser, FailsIfNotStartingArray) { + JsonCompileCommandLineParser ObjectStart("{", NULL); + EXPECT_FALSE(ObjectStart.Parse()) << ObjectStart.GetErrorMessage(); + // We don't implement a full JSON parser, and thus parse only a subset + // of valid JSON. + JsonCompileCommandLineParser Object("{}", NULL); + EXPECT_FALSE(Object.Parse()) << Object.GetErrorMessage(); + JsonCompileCommandLineParser Character("x", NULL); + EXPECT_FALSE(Character.Parse()) << Character.GetErrorMessage(); +} + +TEST(JsonCompileCommandLineParser, ParsesEmptyObject) { + JsonCompileCommandLineParser Parser("[{}]", NULL); + EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage(); +} + +TEST(JsonCompileCommandLineParser, ParsesObject) { + JsonCompileCommandLineParser Parser("[{\"a\":\"/b\"}]", NULL); + EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage(); +} + +TEST(JsonCompileCommandLineParser, ParsesMultipleKeyValuePairsInObject) { + JsonCompileCommandLineParser Parser( + "[{\"a\":\"/b\",\"c\":\"d\",\"e\":\"f\"}]", NULL); + EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage(); +} + +TEST(JsonCompileCommandLineParser, FailsIfNotClosingObject) { + JsonCompileCommandLineParser MissingCloseOnEmpty("[{]", NULL); + EXPECT_FALSE(MissingCloseOnEmpty.Parse()) + << MissingCloseOnEmpty.GetErrorMessage(); + JsonCompileCommandLineParser MissingCloseAfterPair("[{\"a\":\"b\"]", NULL); + EXPECT_FALSE(MissingCloseAfterPair.Parse()) + << MissingCloseAfterPair.GetErrorMessage(); +} + +TEST(JsonCompileCommandLineParser, FailsIfMissingColon) { + JsonCompileCommandLineParser StringString("[{\"a\"\"/b\"}]", NULL); + EXPECT_FALSE(StringString.Parse()) << StringString.GetErrorMessage(); + JsonCompileCommandLineParser StringSpaceString("[{\"a\" \"b\"}]", NULL); + EXPECT_FALSE(StringSpaceString.Parse()) + << StringSpaceString.GetErrorMessage(); +} + +TEST(JsonCompileCommandLineParser, FailsOnMissingQuote) { + JsonCompileCommandLineParser OpenQuote("[{a\":\"b\"}]", NULL); + EXPECT_FALSE(OpenQuote.Parse()) << OpenQuote.GetErrorMessage(); + JsonCompileCommandLineParser CloseQuote("[{\"a\":\"b}]", NULL); + EXPECT_FALSE(CloseQuote.Parse()) << CloseQuote.GetErrorMessage(); +} + +TEST(JsonCompileCommandLineParser, ParsesEscapedQuotes) { + JsonCompileCommandLineParser Parser( + "[{\"a\":\"\\\"b\\\" \\\" \\\"\"}]", NULL); + EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage(); +} + +TEST(JsonCompileCommandLineParser, ParsesEmptyString) { + JsonCompileCommandLineParser Parser("[{\"a\":\"\"}]", NULL); + EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage(); +} + +TEST(JsonCompileCommandLineParser, FailsOnMissingString) { + JsonCompileCommandLineParser MissingValue("[{\"a\":}]", NULL); + EXPECT_FALSE(MissingValue.Parse()) << MissingValue.GetErrorMessage(); + JsonCompileCommandLineParser MissingKey("[{:\"b\"}]", NULL); + EXPECT_FALSE(MissingKey.Parse()) << MissingKey.GetErrorMessage(); +} + +TEST(JsonCompileCommandLineParser, ParsesMultipleObjects) { + JsonCompileCommandLineParser Parser( + "[" + " { \"a\" : \"b\" }," + " { \"a\" : \"b\" }," + " { \"a\" : \"b\" }" + "]", NULL); + EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage(); +} + +TEST(JsonCompileCommandLineParser, FailsOnMissingComma) { + JsonCompileCommandLineParser Parser( + "[" + " { \"a\" : \"b\" }" + " { \"a\" : \"b\" }" + "]", NULL); + EXPECT_FALSE(Parser.Parse()) << Parser.GetErrorMessage(); +} + +TEST(JsonCompileCommandLineParser, FailsOnSuperfluousComma) { + JsonCompileCommandLineParser Parser( + "[ { \"a\" : \"b\" }, ]", NULL); + EXPECT_FALSE(Parser.Parse()) << Parser.GetErrorMessage(); +} + +TEST(JsonCompileCommandLineParser, ParsesSpacesInBetweenTokens) { + JsonCompileCommandLineParser Parser( + " \t \n\n \r [ \t \n\n \r" + " \t \n\n \r { \t \n\n \r\"a\"\t \n\n \r :" + " \t \n\n \r \"b\"\t \n\n \r } \t \n\n \r,\t \n\n \r" + " \t \n\n \r { \t \n\n \r\"a\"\t \n\n \r :" + " \t \n\n \r \"b\"\t \n\n \r } \t \n\n \r]\t \n\n \r", + NULL); + EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage(); +} + +} // end namespace tooling +} // end namespace clang diff --git a/unittests/Tooling/ToolingTest.cpp b/unittests/Tooling/ToolingTest.cpp new file mode 100644 index 0000000..6e5bc6b --- /dev/null +++ b/unittests/Tooling/ToolingTest.cpp @@ -0,0 +1,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 + |