diff --git a/clang/lib/Tooling/Inclusions/HeaderIncludes.cpp b/clang/lib/Tooling/Inclusions/HeaderIncludes.cpp --- a/clang/lib/Tooling/Inclusions/HeaderIncludes.cpp +++ b/clang/lib/Tooling/Inclusions/HeaderIncludes.cpp @@ -100,7 +100,8 @@ [](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 --- a/clang/unittests/Tooling/HeaderIncludesTest.cpp +++ b/clang/unittests/Tooling/HeaderIncludesTest.cpp @@ -347,6 +347,18 @@ EXPECT_EQ(Expected, insert(Code, "")); } +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"