summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHaojian Wu <hokein@google.com>2016-06-03 11:26:02 +0000
committerHaojian Wu <hokein@google.com>2016-06-03 11:26:02 +0000
commit9c6cb035f380fb65061e3618b6f7f9c2b533d01b (patch)
treed9962ed1e8f7b5a32483ebd9165e86c317420c75
parenta6022c9a63036a4e4df490736ff2338581694c67 (diff)
downloadbcm5719-llvm-9c6cb035f380fb65061e3618b6f7f9c2b533d01b.tar.gz
bcm5719-llvm-9c6cb035f380fb65061e3618b6f7f9c2b533d01b.zip
[include-fixer] Don't add missing header if the unindentified symbol isn't from the main file.
Summary: The further solution is to add the missing header to the file where the symbol comes from. Reviewers: bkramer Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D20950 llvm-svn: 271660
-rw-r--r--clang-tools-extra/include-fixer/IncludeFixer.cpp23
-rw-r--r--clang-tools-extra/unittests/include-fixer/IncludeFixerTest.cpp7
2 files changed, 30 insertions, 0 deletions
diff --git a/clang-tools-extra/include-fixer/IncludeFixer.cpp b/clang-tools-extra/include-fixer/IncludeFixer.cpp
index 0dbed36c08e..718e9240a9c 100644
--- a/clang-tools-extra/include-fixer/IncludeFixer.cpp
+++ b/clang-tools-extra/include-fixer/IncludeFixer.cpp
@@ -86,6 +86,29 @@ public:
if (getCompilerInstance().getSema().isSFINAEContext())
return clang::TypoCorrection();
+ // We currently ignore the unidentified symbol which is not from the
+ // main file.
+ //
+ // However, this is not always true due to templates in a non-self contained
+ // header, consider the case:
+ //
+ // // header.h
+ // template <typename T>
+ // class Foo {
+ // T t;
+ // };
+ //
+ // // test.cc
+ // // We need to add <bar.h> in test.cc instead of header.h.
+ // class Bar;
+ // Foo<Bar> foo;
+ //
+ // FIXME: Add the missing header to the header file where the symbol comes
+ // from.
+ if (!getCompilerInstance().getSourceManager().isWrittenInMainFile(
+ Typo.getLoc()))
+ return clang::TypoCorrection();
+
std::string TypoScopeString;
if (S) {
// FIXME: Currently we only use namespace contexts. Use other context
diff --git a/clang-tools-extra/unittests/include-fixer/IncludeFixerTest.cpp b/clang-tools-extra/unittests/include-fixer/IncludeFixerTest.cpp
index 37a741ac7ad..4b9c117ae05 100644
--- a/clang-tools-extra/unittests/include-fixer/IncludeFixerTest.cpp
+++ b/clang-tools-extra/unittests/include-fixer/IncludeFixerTest.cpp
@@ -44,6 +44,8 @@ static bool runOnCode(tooling::ToolAction *ToolAction, StringRef Code,
llvm::MemoryBuffer::getMemBuffer("\n"));
InMemoryFileSystem->addFile("dir/otherdir/qux.h", 0,
llvm::MemoryBuffer::getMemBuffer("\n"));
+ InMemoryFileSystem->addFile("header.h", 0,
+ llvm::MemoryBuffer::getMemBuffer("bar b;"));
return Invocation.run();
}
@@ -186,6 +188,11 @@ TEST(IncludeFixer, EnumConstantSymbols) {
runIncludeFixer("int test = a::b::Green;\n"));
}
+TEST(IncludeFixer, IgnoreSymbolFromHeader) {
+ std::string Code = "#include \"header.h\"";
+ EXPECT_EQ(Code, runIncludeFixer(Code));
+}
+
// FIXME: add test cases for inserting and sorting multiple headers when
// include-fixer supports multiple headers insertion.
TEST(IncludeFixer, InsertAndSortSingleHeader) {
OpenPOWER on IntegriCloud