Skip to content

Commit 9c6cb03

Browse files
committedJun 3, 2016
[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
1 parent a6022c9 commit 9c6cb03

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
 

‎clang-tools-extra/include-fixer/IncludeFixer.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,29 @@ class Action : public clang::ASTFrontendAction,
8686
if (getCompilerInstance().getSema().isSFINAEContext())
8787
return clang::TypoCorrection();
8888

89+
// We currently ignore the unidentified symbol which is not from the
90+
// main file.
91+
//
92+
// However, this is not always true due to templates in a non-self contained
93+
// header, consider the case:
94+
//
95+
// // header.h
96+
// template <typename T>
97+
// class Foo {
98+
// T t;
99+
// };
100+
//
101+
// // test.cc
102+
// // We need to add <bar.h> in test.cc instead of header.h.
103+
// class Bar;
104+
// Foo<Bar> foo;
105+
//
106+
// FIXME: Add the missing header to the header file where the symbol comes
107+
// from.
108+
if (!getCompilerInstance().getSourceManager().isWrittenInMainFile(
109+
Typo.getLoc()))
110+
return clang::TypoCorrection();
111+
89112
std::string TypoScopeString;
90113
if (S) {
91114
// FIXME: Currently we only use namespace contexts. Use other context

‎clang-tools-extra/unittests/include-fixer/IncludeFixerTest.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ static bool runOnCode(tooling::ToolAction *ToolAction, StringRef Code,
4444
llvm::MemoryBuffer::getMemBuffer("\n"));
4545
InMemoryFileSystem->addFile("dir/otherdir/qux.h", 0,
4646
llvm::MemoryBuffer::getMemBuffer("\n"));
47+
InMemoryFileSystem->addFile("header.h", 0,
48+
llvm::MemoryBuffer::getMemBuffer("bar b;"));
4749
return Invocation.run();
4850
}
4951

@@ -186,6 +188,11 @@ TEST(IncludeFixer, EnumConstantSymbols) {
186188
runIncludeFixer("int test = a::b::Green;\n"));
187189
}
188190

191+
TEST(IncludeFixer, IgnoreSymbolFromHeader) {
192+
std::string Code = "#include \"header.h\"";
193+
EXPECT_EQ(Code, runIncludeFixer(Code));
194+
}
195+
189196
// FIXME: add test cases for inserting and sorting multiple headers when
190197
// include-fixer supports multiple headers insertion.
191198
TEST(IncludeFixer, InsertAndSortSingleHeader) {

0 commit comments

Comments
 (0)
Please sign in to comment.