[clang] ea8e71c - [clang][HeaderInsert] Do not treat defines with values as header guards
Kadir Cetinkaya via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 11 07:09:42 PDT 2020
Author: Kadir Cetinkaya
Date: 2020-08-11T16:02:11+02:00
New Revision: ea8e71c3da56f76ae2ab2f9ee7afc6444408ffab
URL: https://github.com/llvm/llvm-project/commit/ea8e71c3da56f76ae2ab2f9ee7afc6444408ffab
DIFF: https://github.com/llvm/llvm-project/commit/ea8e71c3da56f76ae2ab2f9ee7afc6444408ffab.diff
LOG: [clang][HeaderInsert] Do not treat defines with values as header guards
This was resulting in inserting headers at bogus locations, see
https://github.com/ycm-core/YouCompleteMe/issues/3736 for an example.
Differential Revision: https://reviews.llvm.org/D85590
Added:
Modified:
clang/lib/Tooling/Inclusions/HeaderIncludes.cpp
clang/unittests/Tooling/HeaderIncludesTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Tooling/Inclusions/HeaderIncludes.cpp b/clang/lib/Tooling/Inclusions/HeaderIncludes.cpp
index 681fcc5c762a..b65d7f0c1a39 100644
--- a/clang/lib/Tooling/Inclusions/HeaderIncludes.cpp
+++ b/clang/lib/Tooling/Inclusions/HeaderIncludes.cpp
@@ -100,7 +100,8 @@ unsigned getOffsetAfterHeaderGuardsAndComments(StringRef FileName,
[](const SourceManager &SM, Lexer &Lex, Token Tok) -> unsigned {
if (checkAndConsumeDirectiveWithName(Lex, "ifndef", Tok)) {
skipComments(Lex, Tok);
- if (checkAndConsumeDirectiveWithName(Lex, "define", Tok))
+ if (checkAndConsumeDirectiveWithName(Lex, "define", Tok) &&
+ Tok.isAtStartOfLine())
return SM.getFileOffset(Tok.getLocation());
}
return 0;
diff --git a/clang/unittests/Tooling/HeaderIncludesTest.cpp b/clang/unittests/Tooling/HeaderIncludesTest.cpp
index 9f86d2e913a8..d38104fe40ec 100644
--- a/clang/unittests/Tooling/HeaderIncludesTest.cpp
+++ b/clang/unittests/Tooling/HeaderIncludesTest.cpp
@@ -347,6 +347,18 @@ TEST_F(HeaderIncludesTest, FakeHeaderGuard) {
EXPECT_EQ(Expected, insert(Code, "<vector>"));
}
+TEST_F(HeaderIncludesTest, FakeHeaderGuardIfnDef) {
+ std::string Code = "#ifndef A_H\n"
+ "#define A_H 1\n"
+ "#endif";
+ std::string Expected = "#include \"b.h\"\n"
+ "#ifndef A_H\n"
+ "#define A_H 1\n"
+ "#endif";
+
+ EXPECT_EQ(Expected, insert(Code, "\"b.h\""));
+}
+
TEST_F(HeaderIncludesTest, HeaderGuardWithComment) {
std::string Code = "// comment \n"
"#ifndef X // comment\n"
More information about the cfe-commits
mailing list