Skip to content

Commit

Permalink
[include-cleaner] Fix the fixIncludes API not respect main-file hea…
Browse files Browse the repository at this point in the history
…der.

The fixIncludes was using the `input` as the main file path, this will
results in inserting header at wrong places.

We need the main file path to so that we can get the real main-file
header.

Differential Revision: https://reviews.llvm.org/D154950
  • Loading branch information
hokein committed Jul 11, 2023
1 parent 81bc7cf commit 7f3d2cd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ analyze(llvm::ArrayRef<Decl *> ASTRoots,
/// Removes unused includes and inserts missing ones in the main file.
/// Returns the modified main-file code.
/// The FormatStyle must be C++ or ObjC (to support include ordering).
std::string fixIncludes(const AnalysisResults &Results, llvm::StringRef Code,
std::string fixIncludes(const AnalysisResults &Results,
llvm::StringRef FileName, llvm::StringRef Code,
const format::FormatStyle &IncludeStyle);

/// Gets all the providers for a symbol by traversing each location.
Expand Down
7 changes: 4 additions & 3 deletions clang-tools-extra/include-cleaner/lib/Analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,16 @@ analyze(llvm::ArrayRef<Decl *> ASTRoots,
return Results;
}

std::string fixIncludes(const AnalysisResults &Results, llvm::StringRef Code,
std::string fixIncludes(const AnalysisResults &Results,
llvm::StringRef FileName, llvm::StringRef Code,
const format::FormatStyle &Style) {
assert(Style.isCpp() && "Only C++ style supports include insertions!");
tooling::Replacements R;
// Encode insertions/deletions in the magic way clang-format understands.
for (const Include *I : Results.Unused)
cantFail(R.add(tooling::Replacement("input", UINT_MAX, 1, I->quote())));
cantFail(R.add(tooling::Replacement(FileName, UINT_MAX, 1, I->quote())));
for (llvm::StringRef Spelled : Results.Missing)
cantFail(R.add(tooling::Replacement("input", UINT_MAX, 0,
cantFail(R.add(tooling::Replacement(FileName, UINT_MAX, 0,
("#include " + Spelled).str())));
// "cleanup" actually turns the UINT_MAX replacements into concrete edits.
auto Positioned = cantFail(format::cleanupAroundReplacements(Code, R, Style));
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class Action : public clang::ASTFrontendAction {
Results.Missing.clear();
if (!Remove)
Results.Unused.clear();
std::string Final = fixIncludes(Results, Code, getStyle(Path));
std::string Final = fixIncludes(Results, Path, Code, getStyle(Path));

if (Print.getNumOccurrences()) {
switch (Print) {
Expand Down
15 changes: 13 additions & 2 deletions clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ TEST_F(AnalyzeTest, NoCrashWhenUnresolved) {
}

TEST(FixIncludes, Basic) {
llvm::StringRef Code = R"cpp(
llvm::StringRef Code = R"cpp(#include "d.h"
#include "a.h"
#include "b.h"
#include <c.h>
Expand All @@ -300,11 +300,22 @@ TEST(FixIncludes, Basic) {
Results.Unused.push_back(Inc.atLine(3));
Results.Unused.push_back(Inc.atLine(4));

EXPECT_EQ(fixIncludes(Results, Code, format::getLLVMStyle()), R"cpp(
EXPECT_EQ(fixIncludes(Results, "d.cc", Code, format::getLLVMStyle()),
R"cpp(#include "d.h"
#include "a.h"
#include "aa.h"
#include "ab.h"
#include <e.h>
)cpp");

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");
}

Expand Down

0 comments on commit 7f3d2cd

Please sign in to comment.