Skip to content

Commit

Permalink
[include-cleaner] Avoid a caching issue when running --edit mode on m…
Browse files Browse the repository at this point in the history
…ultiple files.

Snapshot all analysing files before running the tool, this makes sure
that we analyse all files statelessly and avoid the FileManager caching issue
when running `-edit` on multiple files.

Differential Revision: https://reviews.llvm.org/D155195
  • Loading branch information
hokein committed Jul 18, 2023
1 parent fc43c4f commit be861b6
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/raw_ostream.h"
Expand Down Expand Up @@ -270,12 +271,24 @@ int main(int argc, const char **argv) {
}
}
}

clang::tooling::ClangTool Tool(OptionsParser->getCompilations(),
OptionsParser->getSourcePathList());
std::vector<std::unique_ptr<llvm::MemoryBuffer>> Buffers;
for (const auto &File : OptionsParser->getSourcePathList()) {
auto Content = llvm::MemoryBuffer::getFile(File);
if (!Content) {
llvm::errs() << "Error: can't read file '" << File
<< "': " << Content.getError().message() << "\n";
return 1;
}
Buffers.push_back(std::move(Content.get()));
Tool.mapVirtualFile(File, Buffers.back()->getBuffer());
}

auto HeaderFilter = headerFilter();
if (!HeaderFilter)
return 1; // error already reported.
ActionFactory Factory(HeaderFilter);
return clang::tooling::ClangTool(OptionsParser->getCompilations(),
OptionsParser->getSourcePathList())
.run(&Factory) ||
Errors != 0;
return Tool.run(&Factory) || Errors != 0;
}

0 comments on commit be861b6

Please sign in to comment.