diff options
author | Haojian Wu <hokein@google.com> | 2016-07-08 09:10:29 +0000 |
---|---|---|
committer | Haojian Wu <hokein@google.com> | 2016-07-08 09:10:29 +0000 |
commit | 627ca96e4d259d50c5e6f740140d6dac9f008852 (patch) | |
tree | 87dc6f9e9d32813a3761e0b6f985f49a16116c1c /clang-tools-extra/unittests/include-fixer/IncludeFixerTest.cpp | |
parent | 7baef47065fa7749520514df47f807f449e0177c (diff) | |
download | bcm5719-llvm-627ca96e4d259d50c5e6f740140d6dac9f008852.tar.gz bcm5719-llvm-627ca96e4d259d50c5e6f740140d6dac9f008852.zip |
[include-fixer] Add missing namespace qualifiers after inserting a missing header.
Summary:
This is an initial version of fixing namespace issues by adding missing
namespace qualifiers to an unidentified symbol.
This version only fixes the first discovered unidentified symbol.
In the long run, include-fixer should fix all unidentified symbols
with a same name at one run.
Currently, it works on command-line tool. The vim integration is not
implemented yet.
Reviewers: klimek, bkramer, djasper
Subscribers: bkramer, ioeric, cfe-commits
Differential Revision: http://reviews.llvm.org/D21603
llvm-svn: 274832
Diffstat (limited to 'clang-tools-extra/unittests/include-fixer/IncludeFixerTest.cpp')
-rw-r--r-- | clang-tools-extra/unittests/include-fixer/IncludeFixerTest.cpp | 61 |
1 files changed, 48 insertions, 13 deletions
diff --git a/clang-tools-extra/unittests/include-fixer/IncludeFixerTest.cpp b/clang-tools-extra/unittests/include-fixer/IncludeFixerTest.cpp index d0bdccacc7c..effed6df14a 100644 --- a/clang-tools-extra/unittests/include-fixer/IncludeFixerTest.cpp +++ b/clang-tools-extra/unittests/include-fixer/IncludeFixerTest.cpp @@ -82,14 +82,17 @@ static std::string runIncludeFixer( IncludeFixerContext FixerContext; IncludeFixerActionFactory Factory(*SymbolIndexMgr, FixerContext, "llvm"); - runOnCode(&Factory, Code, "input.cc", ExtraArgs); - if (FixerContext.Headers.empty()) + std::string FakeFileName = "input.cc"; + runOnCode(&Factory, Code, FakeFileName, ExtraArgs); + if (FixerContext.getMatchedSymbols().empty()) return Code; tooling::Replacements Replacements = clang::include_fixer::createInsertHeaderReplacements( - Code, "input.cc", FixerContext.Headers.front()); + Code, FakeFileName, FixerContext.getHeaders().front()); clang::RewriterTestContext Context; - clang::FileID ID = Context.createInMemoryFile("input.cc", Code); + clang::FileID ID = Context.createInMemoryFile(FakeFileName, Code); + if (FixerContext.getSymbolRange().getLength() > 0) + Replacements.insert(FixerContext.createSymbolReplacement(FakeFileName, 0)); clang::tooling::applyAllReplacements(Replacements, Context.Rewrite); return Context.getRewrittenText(ID); } @@ -113,15 +116,9 @@ TEST(IncludeFixer, Typo) { "#include \"foo.h\"\n#include <string>\nstd::string::size_type foo;\n", runIncludeFixer("#include \"foo.h\"\nstd::string::size_type foo;\n")); - // string without "std::" can also be fixed since fixed db results go through - // SymbolIndexManager, and SymbolIndexManager matches unqualified identifiers - // too. - EXPECT_EQ("#include <string>\nstring foo;\n", + EXPECT_EQ("#include <string>\nstd::string foo;\n", runIncludeFixer("string foo;\n")); - // Fully qualified name. - EXPECT_EQ("#include <string>\n::std::string foo;\n", - runIncludeFixer("::std::string foo;\n")); // Should not match std::string. EXPECT_EQ("::string foo;\n", runIncludeFixer("::string foo;\n")); } @@ -129,7 +126,7 @@ TEST(IncludeFixer, Typo) { TEST(IncludeFixer, IncompleteType) { EXPECT_EQ( "#include \"foo.h\"\n#include <string>\n" - "namespace std {\nclass string;\n}\nstring foo;\n", + "namespace std {\nclass string;\n}\nstd::string foo;\n", runIncludeFixer("#include \"foo.h\"\n" "namespace std {\nclass string;\n}\nstring foo;\n")); } @@ -186,7 +183,7 @@ TEST(IncludeFixer, ScopedNamespaceSymbols) { runIncludeFixer("namespace A { c::b::bar b; }\n")); // FIXME: The header should not be added here. Remove this after we support // full match. - EXPECT_EQ("#include \"bar.h\"\nnamespace A {\nb::bar b;\n}", + EXPECT_EQ("#include \"bar.h\"\nnamespace A {\na::b::bar b;\n}", runIncludeFixer("namespace A {\nb::bar b;\n}")); } @@ -221,6 +218,44 @@ TEST(IncludeFixer, DoNotDeleteMatchedSymbol) { runIncludeFixer("a::Vector v;")); } +TEST(IncludeFixer, FixNamespaceQualifiers) { + EXPECT_EQ("#include \"bar.h\"\na::b::bar b;\n", + runIncludeFixer("b::bar b;\n")); + EXPECT_EQ("#include \"bar.h\"\na::b::bar b;\n", + runIncludeFixer("a::b::bar b;\n")); + EXPECT_EQ("#include \"bar.h\"\na::b::bar b;\n", + runIncludeFixer("bar b;\n")); + EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar b;\n}\n", + runIncludeFixer("namespace a {\nb::bar b;\n}\n")); + EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar b;\n}\n", + runIncludeFixer("namespace a {\nbar b;\n}\n")); + EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nnamespace b{\nbar b;\n}\n}\n", + runIncludeFixer("namespace a {\nnamespace b{\nbar b;\n}\n}\n")); + EXPECT_EQ("c::b::bar b;\n", + runIncludeFixer("c::b::bar b;\n")); + EXPECT_EQ("#include \"bar.h\"\nnamespace c {\na::b::bar b;\n}\n", + runIncludeFixer("namespace c {\nbar b;\n}\n")); + + // Test nested classes. + EXPECT_EQ("#include \"bar.h\"\nnamespace c {\na::b::bar::t b;\n}\n", + runIncludeFixer("namespace c {\nbar::t b;\n}\n")); + EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar::t b;\n}\n", + runIncludeFixer("namespace a {\nbar::t b;\n}\n")); + + EXPECT_EQ( + "#include \"color.h\"\nint test = a::b::Green;\n", + runIncludeFixer("int test = Green;\n")); + EXPECT_EQ("#include \"color.h\"\nnamespace d {\nint test = a::b::Green;\n}\n", + runIncludeFixer("namespace d {\nint test = Green;\n}\n")); + EXPECT_EQ("#include \"color.h\"\nnamespace a {\nint test = b::Green;\n}\n", + runIncludeFixer("namespace a {\nint test = Green;\n}\n")); + + // FIXME: Fix-namespace should not fix the global qualified identifier. + EXPECT_EQ( + "#include \"bar.h\"\na::b::bar b;\n", + runIncludeFixer("::a::b::bar b;\n")); +} + } // namespace } // namespace include_fixer } // namespace clang |