This revision was landed with ongoing or failed builds. This revision was automatically updated to reflect the committed changes. Closed by commit rGea8e71c3da56: [clang][HeaderInsert] Do not treat defines with values as header guards (authored by kadircet).
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D85590/new/ https://reviews.llvm.org/D85590 Files: clang/lib/Tooling/Inclusions/HeaderIncludes.cpp clang/unittests/Tooling/HeaderIncludesTest.cpp Index: clang/unittests/Tooling/HeaderIncludesTest.cpp =================================================================== --- clang/unittests/Tooling/HeaderIncludesTest.cpp +++ clang/unittests/Tooling/HeaderIncludesTest.cpp @@ -347,6 +347,18 @@ 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" Index: clang/lib/Tooling/Inclusions/HeaderIncludes.cpp =================================================================== --- clang/lib/Tooling/Inclusions/HeaderIncludes.cpp +++ 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;
Index: clang/unittests/Tooling/HeaderIncludesTest.cpp =================================================================== --- clang/unittests/Tooling/HeaderIncludesTest.cpp +++ clang/unittests/Tooling/HeaderIncludesTest.cpp @@ -347,6 +347,18 @@ 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" Index: clang/lib/Tooling/Inclusions/HeaderIncludes.cpp =================================================================== --- clang/lib/Tooling/Inclusions/HeaderIncludes.cpp +++ 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;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits