[PATCH] D154963: [Format][Tooling] Fix HeaderIncludes::insert not respect the main-file header.
Haojian Wu via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 11 06:35:25 PDT 2023
hokein created this revision.
hokein added reviewers: sammccall, kadircet.
Herald added a project: All.
hokein requested review of this revision.
Herald added projects: clang, clang-tools-extra.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D154963
Files:
clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
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
@@ -143,6 +143,17 @@
EXPECT_NE(Expected, insert(Code, "<a>")) << "Not main header";
}
+TEST_F(HeaderIncludesTest, InsertMainHeader) {
+ std::string Code = R"cpp(#include "a.h")cpp";
+ std::string Expected = R"cpp(#include "fix.h"
+#include "a.h")cpp";
+
+ Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp)
+ .IncludeStyle;
+ FileName = "fix.cpp";
+ EXPECT_EQ(Expected, insert(Code, "\"fix.h\""));
+}
+
TEST_F(HeaderIncludesTest, InsertBeforeSystemHeaderLLVM) {
std::string Code = "#include <memory>\n"
"\n"
Index: clang/lib/Tooling/Inclusions/HeaderIncludes.cpp
===================================================================
--- clang/lib/Tooling/Inclusions/HeaderIncludes.cpp
+++ clang/lib/Tooling/Inclusions/HeaderIncludes.cpp
@@ -335,7 +335,7 @@
// Only record the offset of current #include if we can insert after it.
if (CurInclude.R.getOffset() <= MaxInsertOffset) {
int Priority = Categories.getIncludePriority(
- CurInclude.Name, /*CheckMainHeader=*/FirstIncludeOffset < 0);
+ CurInclude.Name, /*CheckMainHeader=*/true);
CategoryEndOffsets[Priority] = NextLineOffset;
IncludesByPriority[Priority].push_back(&CurInclude);
if (FirstIncludeOffset < 0)
@@ -362,7 +362,7 @@
std::string(llvm::formatv(IsAngled ? "<{0}>" : "\"{0}\"", IncludeName));
StringRef QuotedName = Quoted;
int Priority = Categories.getIncludePriority(
- QuotedName, /*CheckMainHeader=*/FirstIncludeOffset < 0);
+ QuotedName, /*CheckMainHeader=*/true);
auto CatOffset = CategoryEndOffsets.find(Priority);
assert(CatOffset != CategoryEndOffsets.end());
unsigned InsertOffset = CatOffset->second; // Fall back offset
Index: clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
===================================================================
--- clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
@@ -311,12 +311,9 @@
Results = {};
Results.Missing.push_back("\"d.h\"");
Code = R"cpp(#include "a.h")cpp";
- // FIXME: this isn't correct, the main-file header d.h should be added before
- // a.h.
EXPECT_EQ(fixIncludes(Results, "d.cc", Code, format::getLLVMStyle()),
-R"cpp(#include "a.h"
-#include "d.h"
-)cpp");
+R"cpp(#include "d.h"
+#include "a.h")cpp");
}
MATCHER_P3(expandedAt, FileID, Offset, SM, "") {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154963.539065.patch
Type: text/x-patch
Size: 2658 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230711/b9604bbf/attachment.bin>
More information about the cfe-commits
mailing list