diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2017-06-16 21:03:44 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2017-06-16 21:03:44 +0000 |
commit | 325377b57338e700317f5e423e5b0f1c08d99a39 (patch) | |
tree | acd401a9713562cf3e93d13fa6a70ad67eb5cd99 /unittests | |
parent | 1b08b196ac845675036ac78f3ac927d0a37f707c (diff) |
Notes
Diffstat (limited to 'unittests')
-rw-r--r-- | unittests/AST/CommentLexer.cpp | 3 | ||||
-rw-r--r-- | unittests/ASTMatchers/ASTMatchersTest.h | 9 | ||||
-rw-r--r-- | unittests/Basic/VirtualFileSystemTest.cpp | 6 | ||||
-rw-r--r-- | unittests/Format/FormatTest.cpp | 261 | ||||
-rw-r--r-- | unittests/Format/NamespaceEndCommentsFixerTest.cpp | 56 | ||||
-rw-r--r-- | unittests/Lex/LexerTest.cpp | 73 | ||||
-rw-r--r-- | unittests/Tooling/LookupTest.cpp | 6 |
7 files changed, 397 insertions, 17 deletions
diff --git a/unittests/AST/CommentLexer.cpp b/unittests/AST/CommentLexer.cpp index 77ee22ffb43e1..f96d6cd15f7a9 100644 --- a/unittests/AST/CommentLexer.cpp +++ b/unittests/AST/CommentLexer.cpp @@ -320,9 +320,10 @@ TEST_F(CommentLexerTest, DoxygenCommand4) { ASSERT_EQ(array_lengthof(Text), Toks.size()); for (size_t j = 0, e = Toks.size(); j != e; j++) { - if(Toks[j].is(tok::text)) + if(Toks[j].is(tok::text)) { ASSERT_EQ(StringRef(Text[j]), Toks[j].getText()) << "index " << i; + } } } } diff --git a/unittests/ASTMatchers/ASTMatchersTest.h b/unittests/ASTMatchers/ASTMatchersTest.h index 4f5579ce0def1..7cfe5b9e37097 100644 --- a/unittests/ASTMatchers/ASTMatchersTest.h +++ b/unittests/ASTMatchers/ASTMatchersTest.h @@ -320,10 +320,12 @@ public: ExpectedName(ExpectedName) {} void onEndOfTranslationUnit() override { - if (ExpectedCount != -1) + if (ExpectedCount != -1) { EXPECT_EQ(ExpectedCount, Count); - if (!ExpectedName.empty()) + } + if (!ExpectedName.empty()) { EXPECT_EQ(ExpectedName, Name); + } Count = 0; Name.clear(); } @@ -346,8 +348,9 @@ public: } BoundNodes::IDToNodeMap::const_iterator I = M.find(Id); EXPECT_NE(M.end(), I); - if (I != M.end()) + if (I != M.end()) { EXPECT_EQ(Nodes->getNodeAs<T>(Id), I->second.get<T>()); + } return true; } EXPECT_TRUE(M.count(Id) == 0 || diff --git a/unittests/Basic/VirtualFileSystemTest.cpp b/unittests/Basic/VirtualFileSystemTest.cpp index 0856b17791fa9..40add2195b586 100644 --- a/unittests/Basic/VirtualFileSystemTest.cpp +++ b/unittests/Basic/VirtualFileSystemTest.cpp @@ -300,8 +300,9 @@ struct ScopedDir { EXPECT_FALSE(EC); } ~ScopedDir() { - if (Path != "") + if (Path != "") { EXPECT_FALSE(llvm::sys::fs::remove(Path.str())); + } } operator StringRef() { return Path.str(); } }; @@ -316,8 +317,9 @@ struct ScopedLink { EXPECT_FALSE(EC); } ~ScopedLink() { - if (Path != "") + if (Path != "") { EXPECT_FALSE(llvm::sys::fs::remove(Path.str())); + } } operator StringRef() { return Path.str(); } }; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 64e963db4cfde..f4937171038d2 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -1310,6 +1310,141 @@ TEST_F(FormatTest, FormatsNamespaces) { Style)); } +TEST_F(FormatTest, FormatsCompactNamespaces) { + FormatStyle Style = getLLVMStyle(); + Style.CompactNamespaces = true; + + verifyFormat("namespace A { namespace B {\n" + "}} // namespace A::B", + Style); + + EXPECT_EQ("namespace out { namespace in {\n" + "}} // namespace out::in", + format("namespace out {\n" + "namespace in {\n" + "} // namespace in\n" + "} // namespace out", + Style)); + + // Only namespaces which have both consecutive opening and end get compacted + EXPECT_EQ("namespace out {\n" + "namespace in1 {\n" + "} // namespace in1\n" + "namespace in2 {\n" + "} // namespace in2\n" + "} // namespace out", + format("namespace out {\n" + "namespace in1 {\n" + "} // namespace in1\n" + "namespace in2 {\n" + "} // namespace in2\n" + "} // namespace out", + Style)); + + EXPECT_EQ("namespace out {\n" + "int i;\n" + "namespace in {\n" + "int j;\n" + "} // namespace in\n" + "int k;\n" + "} // namespace out", + format("namespace out { int i;\n" + "namespace in { int j; } // namespace in\n" + "int k; } // namespace out", + Style)); + + EXPECT_EQ("namespace A { namespace B { namespace C {\n" + "}}} // namespace A::B::C\n", + format("namespace A { namespace B {\n" + "namespace C {\n" + "}} // namespace B::C\n" + "} // namespace A\n", + Style)); + + Style.ColumnLimit = 40; + EXPECT_EQ("namespace aaaaaaaaaa {\n" + "namespace bbbbbbbbbb {\n" + "}} // namespace aaaaaaaaaa::bbbbbbbbbb", + format("namespace aaaaaaaaaa {\n" + "namespace bbbbbbbbbb {\n" + "} // namespace bbbbbbbbbb\n" + "} // namespace aaaaaaaaaa", + Style)); + + EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" + "namespace cccccc {\n" + "}}} // namespace aaaaaa::bbbbbb::cccccc", + format("namespace aaaaaa {\n" + "namespace bbbbbb {\n" + "namespace cccccc {\n" + "} // namespace cccccc\n" + "} // namespace bbbbbb\n" + "} // namespace aaaaaa", + Style)); + Style.ColumnLimit = 80; + + // Extra semicolon after 'inner' closing brace prevents merging + EXPECT_EQ("namespace out { namespace in {\n" + "}; } // namespace out::in", + format("namespace out {\n" + "namespace in {\n" + "}; // namespace in\n" + "} // namespace out", + Style)); + + // Extra semicolon after 'outer' closing brace is conserved + EXPECT_EQ("namespace out { namespace in {\n" + "}}; // namespace out::in", + format("namespace out {\n" + "namespace in {\n" + "} // namespace in\n" + "}; // namespace out", + Style)); + + Style.NamespaceIndentation = FormatStyle::NI_All; + EXPECT_EQ("namespace out { namespace in {\n" + " int i;\n" + "}} // namespace out::in", + format("namespace out {\n" + "namespace in {\n" + "int i;\n" + "} // namespace in\n" + "} // namespace out", + Style)); + EXPECT_EQ("namespace out { namespace mid {\n" + " namespace in {\n" + " int j;\n" + " } // namespace in\n" + " int k;\n" + "}} // namespace out::mid", + format("namespace out { namespace mid {\n" + "namespace in { int j; } // namespace in\n" + "int k; }} // namespace out::mid", + Style)); + + Style.NamespaceIndentation = FormatStyle::NI_Inner; + EXPECT_EQ("namespace out { namespace in {\n" + " int i;\n" + "}} // namespace out::in", + format("namespace out {\n" + "namespace in {\n" + "int i;\n" + "} // namespace in\n" + "} // namespace out", + Style)); + EXPECT_EQ("namespace out { namespace mid { namespace in {\n" + " int i;\n" + "}}} // namespace out::mid::in", + format("namespace out {\n" + "namespace mid {\n" + "namespace in {\n" + "int i;\n" + "} // namespace in\n" + "} // namespace mid\n" + "} // namespace out", + Style)); +} + TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } TEST_F(FormatTest, FormatsInlineASM) { @@ -6239,6 +6374,35 @@ TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { getLLVMStyleWithColumns(23)); } +TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { + FormatStyle MergeEmptyOnly = getLLVMStyle(); + MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; + verifyFormat("class C {\n" + " int f() {}\n" + "};", + MergeEmptyOnly); + verifyFormat("class C {\n" + " int f() {\n" + " return 42;\n" + " }\n" + "};", + MergeEmptyOnly); + verifyFormat("int f() {}", MergeEmptyOnly); + verifyFormat("int f() {\n" + " return 42;\n" + "}", + MergeEmptyOnly); + + // Also verify behavior when BraceWrapping.AfterFunction = true + MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; + MergeEmptyOnly.BraceWrapping.AfterFunction = true; + verifyFormat("int f() {}", MergeEmptyOnly); + verifyFormat("class C {\n" + " int f() {}\n" + "};", + MergeEmptyOnly); +} + TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { FormatStyle MergeInlineOnly = getLLVMStyle(); MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; @@ -6250,6 +6414,101 @@ TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { " return 42;\n" "}", MergeInlineOnly); + + // SFS_Inline implies SFS_Empty + verifyFormat("class C {\n" + " int f() {}\n" + "};", + MergeInlineOnly); + verifyFormat("int f() {}", MergeInlineOnly); + + // Also verify behavior when BraceWrapping.AfterFunction = true + MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; + MergeInlineOnly.BraceWrapping.AfterFunction = true; + verifyFormat("class C {\n" + " int f() { return 42; }\n" + "};", + MergeInlineOnly); + verifyFormat("int f()\n" + "{\n" + " return 42;\n" + "}", + MergeInlineOnly); + + // SFS_Inline implies SFS_Empty + verifyFormat("int f() {}", MergeInlineOnly); + verifyFormat("class C {\n" + " int f() {}\n" + "};", + MergeInlineOnly); +} + +TEST_F(FormatTest, SplitEmptyFunctionBody) { + FormatStyle Style = getLLVMStyle(); + Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; + Style.BreakBeforeBraces = FormatStyle::BS_Custom; + Style.BraceWrapping.AfterFunction = true; + Style.BraceWrapping.SplitEmptyFunctionBody = false; + Style.ColumnLimit = 40; + + verifyFormat("int f()\n" + "{}", + Style); + verifyFormat("int f()\n" + "{\n" + " return 42;\n" + "}", + Style); + verifyFormat("int f()\n" + "{\n" + " // some comment\n" + "}", + Style); + + Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; + verifyFormat("int f() {}", Style); + verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" + "{}", + Style); + verifyFormat("int f()\n" + "{\n" + " return 0;\n" + "}", + Style); + + Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; + verifyFormat("class Foo {\n" + " int f() {}\n" + "};\n", + Style); + verifyFormat("class Foo {\n" + " int f() { return 0; }\n" + "};\n", + Style); + verifyFormat("class Foo {\n" + " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" + " {}\n" + "};\n", + Style); + verifyFormat("class Foo {\n" + " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" + " {\n" + " return 0;\n" + " }\n" + "};\n", + Style); + + Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; + verifyFormat("int f() {}", Style); + verifyFormat("int f() { return 0; }", Style); + verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" + "{}", + Style); + verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" + "{\n" + " return 0;\n" + "}", + Style); } TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { @@ -8927,6 +9186,7 @@ TEST_F(FormatTest, ParsesConfigurationBools) { CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); CHECK_PARSE_BOOL(BreakStringLiterals); CHECK_PARSE_BOOL(BreakBeforeInheritanceComma) + CHECK_PARSE_BOOL(CompactNamespaces); CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); CHECK_PARSE_BOOL(DerivePointerAlignment); CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); @@ -8960,6 +9220,7 @@ TEST_F(FormatTest, ParsesConfigurationBools) { CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); + CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunctionBody); } #undef CHECK_PARSE_BOOL diff --git a/unittests/Format/NamespaceEndCommentsFixerTest.cpp b/unittests/Format/NamespaceEndCommentsFixerTest.cpp index 6c2d369852904..92f3421629387 100644 --- a/unittests/Format/NamespaceEndCommentsFixerTest.cpp +++ b/unittests/Format/NamespaceEndCommentsFixerTest.cpp @@ -185,6 +185,41 @@ TEST_F(NamespaceEndCommentsFixerTest, AddsEndComment) { "}\n" "}")); + // Add comment for namespaces which will be 'compacted' + FormatStyle CompactNamespacesStyle = getLLVMStyle(); + CompactNamespacesStyle.CompactNamespaces = true; + EXPECT_EQ("namespace out { namespace in {\n" + "int i;\n" + "int j;\n" + "}}// namespace out::in", + fixNamespaceEndComments("namespace out { namespace in {\n" + "int i;\n" + "int j;\n" + "}}", + CompactNamespacesStyle)); + EXPECT_EQ("namespace out {\n" + "namespace in {\n" + "int i;\n" + "int j;\n" + "}\n" + "}// namespace out::in", + fixNamespaceEndComments("namespace out {\n" + "namespace in {\n" + "int i;\n" + "int j;\n" + "}\n" + "}", + CompactNamespacesStyle)); + EXPECT_EQ("namespace out { namespace in {\n" + "int i;\n" + "int j;\n" + "};}// namespace out::in", + fixNamespaceEndComments("namespace out { namespace in {\n" + "int i;\n" + "int j;\n" + "};}", + CompactNamespacesStyle)); + // Adds an end comment after a semicolon. EXPECT_EQ("namespace {\n" " int i;\n" @@ -388,6 +423,27 @@ TEST_F(NamespaceEndCommentsFixerTest, UpdatesInvalidEndLineComment) { fixNamespaceEndComments("namespace A {} // namespace")); EXPECT_EQ("namespace A {}; // namespace A", fixNamespaceEndComments("namespace A {}; // namespace")); + + // Update invalid comments for compacted namespaces. + FormatStyle CompactNamespacesStyle = getLLVMStyle(); + CompactNamespacesStyle.CompactNamespaces = true; + EXPECT_EQ("namespace out { namespace in {\n" + "}} // namespace out::in", + fixNamespaceEndComments("namespace out { namespace in {\n" + "}} // namespace out", + CompactNamespacesStyle)); + EXPECT_EQ("namespace out { namespace in {\n" + "}} // namespace out::in", + fixNamespaceEndComments("namespace out { namespace in {\n" + "}} // namespace in", + CompactNamespacesStyle)); + EXPECT_EQ("namespace out { namespace in {\n" + "}\n" + "} // namespace out::in", + fixNamespaceEndComments("namespace out { namespace in {\n" + "}// banamespace in\n" + "} // namespace out", + CompactNamespacesStyle)); } TEST_F(NamespaceEndCommentsFixerTest, UpdatesInvalidEndBlockComment) { diff --git a/unittests/Lex/LexerTest.cpp b/unittests/Lex/LexerTest.cpp index e2507d3580d37..a887c22bd301e 100644 --- a/unittests/Lex/LexerTest.cpp +++ b/unittests/Lex/LexerTest.cpp @@ -18,6 +18,8 @@ #include "clang/Basic/TargetOptions.h" #include "clang/Lex/HeaderSearch.h" #include "clang/Lex/HeaderSearchOptions.h" +#include "clang/Lex/MacroArgs.h" +#include "clang/Lex/MacroInfo.h" #include "clang/Lex/ModuleLoader.h" #include "clang/Lex/Preprocessor.h" #include "clang/Lex/PreprocessorOptions.h" @@ -41,26 +43,33 @@ protected: Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts); } - std::vector<Token> Lex(StringRef Source) { + std::unique_ptr<Preprocessor> CreatePP(StringRef Source, + TrivialModuleLoader &ModLoader) { std::unique_ptr<llvm::MemoryBuffer> Buf = llvm::MemoryBuffer::getMemBuffer(Source); SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf))); - TrivialModuleLoader ModLoader; MemoryBufferCache PCMCache; HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr, Diags, LangOpts, Target.get()); - Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts, - SourceMgr, PCMCache, HeaderInfo, ModLoader, - /*IILookup =*/nullptr, - /*OwnsHeaderSearch =*/false); - PP.Initialize(*Target); - PP.EnterMainSourceFile(); + std::unique_ptr<Preprocessor> PP = llvm::make_unique<Preprocessor>( + std::make_shared<PreprocessorOptions>(), Diags, LangOpts, SourceMgr, + PCMCache, HeaderInfo, ModLoader, + /*IILookup =*/nullptr, + /*OwnsHeaderSearch =*/false); + PP->Initialize(*Target); + PP->EnterMainSourceFile(); + return PP; + } + + std::vector<Token> Lex(StringRef Source) { + TrivialModuleLoader ModLoader; + auto PP = CreatePP(Source, ModLoader); std::vector<Token> toks; while (1) { Token tok; - PP.Lex(tok); + PP->Lex(tok); if (tok.is(tok::eof)) break; toks.push_back(tok); @@ -365,4 +374,50 @@ TEST_F(LexerTest, DontMergeMacroArgsFromDifferentMacroFiles) { EXPECT_EQ(SourceMgr.getFileIDSize(SourceMgr.getFileID(helper1ArgLoc)), 8U); } +TEST_F(LexerTest, DontOverallocateStringifyArgs) { + TrivialModuleLoader ModLoader; + auto PP = CreatePP("\"StrArg\", 5, 'C'", ModLoader); + + llvm::BumpPtrAllocator Allocator; + std::array<IdentifierInfo *, 3> ArgList; + MacroInfo *MI = PP->AllocateMacroInfo({}); + MI->setIsFunctionLike(); + MI->setArgumentList(ArgList, Allocator); + EXPECT_EQ(3u, MI->getNumArgs()); + EXPECT_TRUE(MI->isFunctionLike()); + + Token Eof; + Eof.setKind(tok::eof); + std::vector<Token> ArgTokens; + while (1) { + Token tok; + PP->Lex(tok); + if (tok.is(tok::eof)) { + ArgTokens.push_back(Eof); + break; + } + if (tok.is(tok::comma)) + ArgTokens.push_back(Eof); + else + ArgTokens.push_back(tok); + } + + auto MacroArgsDeleter = [&PP](MacroArgs *M) { M->destroy(*PP); }; + std::unique_ptr<MacroArgs, decltype(MacroArgsDeleter)> MA( + MacroArgs::create(MI, ArgTokens, false, *PP), MacroArgsDeleter); + Token Result = MA->getStringifiedArgument(0, *PP, {}, {}); + EXPECT_EQ(tok::string_literal, Result.getKind()); + EXPECT_STREQ("\"\\\"StrArg\\\"\"", Result.getLiteralData()); + Result = MA->getStringifiedArgument(1, *PP, {}, {}); + EXPECT_EQ(tok::string_literal, Result.getKind()); + EXPECT_STREQ("\"5\"", Result.getLiteralData()); + Result = MA->getStringifiedArgument(2, *PP, {}, {}); + EXPECT_EQ(tok::string_literal, Result.getKind()); + EXPECT_STREQ("\"'C'\"", Result.getLiteralData()); +#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST + EXPECT_DEATH(MA->getStringifiedArgument(3, *PP, {}, {}), + "Invalid argument number!"); +#endif +} + } // anonymous namespace diff --git a/unittests/Tooling/LookupTest.cpp b/unittests/Tooling/LookupTest.cpp index cc3922d01b51c..f42f31e9dd999 100644 --- a/unittests/Tooling/LookupTest.cpp +++ b/unittests/Tooling/LookupTest.cpp @@ -143,8 +143,9 @@ TEST(LookupTest, replaceNestedClassName) { Visitor.OnRecordTypeLoc = [&](RecordTypeLoc Type) { // Filter Types by name since there are other `RecordTypeLoc` in the test // file. - if (Type.getDecl()->getQualifiedNameAsString() == "a::b::Foo") + if (Type.getDecl()->getQualifiedNameAsString() == "a::b::Foo") { EXPECT_EQ("x::Bar", replaceRecordTypeLoc(Type, "::a::x::Bar")); + } }; Visitor.runOver("namespace a { namespace b {\n" "class Foo;\n" @@ -155,8 +156,9 @@ TEST(LookupTest, replaceNestedClassName) { // Filter Types by name since there are other `RecordTypeLoc` in the test // file. // `a::b::Foo` in using shadow decl is not `TypeLoc`. - if (Type.getDecl()->getQualifiedNameAsString() == "a::b::Foo") + if (Type.getDecl()->getQualifiedNameAsString() == "a::b::Foo") { EXPECT_EQ("Bar", replaceRecordTypeLoc(Type, "::a::x::Bar")); + } }; Visitor.runOver("namespace a { namespace b { class Foo {}; } }\n" "namespace c { using a::b::Foo; Foo f();; }\n"); |