Skip to content

Commit be861b6

Browse files
committed
[include-cleaner] Avoid a caching issue when running --edit mode on multiple 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
1 parent fc43c4f commit be861b6

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/ADT/StringRef.h"
2020
#include "llvm/Support/CommandLine.h"
2121
#include "llvm/Support/FormatVariadic.h"
22+
#include "llvm/Support/MemoryBuffer.h"
2223
#include "llvm/Support/Regex.h"
2324
#include "llvm/Support/Signals.h"
2425
#include "llvm/Support/raw_ostream.h"
@@ -270,12 +271,24 @@ int main(int argc, const char **argv) {
270271
}
271272
}
272273
}
274+
275+
clang::tooling::ClangTool Tool(OptionsParser->getCompilations(),
276+
OptionsParser->getSourcePathList());
277+
std::vector<std::unique_ptr<llvm::MemoryBuffer>> Buffers;
278+
for (const auto &File : OptionsParser->getSourcePathList()) {
279+
auto Content = llvm::MemoryBuffer::getFile(File);
280+
if (!Content) {
281+
llvm::errs() << "Error: can't read file '" << File
282+
<< "': " << Content.getError().message() << "\n";
283+
return 1;
284+
}
285+
Buffers.push_back(std::move(Content.get()));
286+
Tool.mapVirtualFile(File, Buffers.back()->getBuffer());
287+
}
288+
273289
auto HeaderFilter = headerFilter();
274290
if (!HeaderFilter)
275291
return 1; // error already reported.
276292
ActionFactory Factory(HeaderFilter);
277-
return clang::tooling::ClangTool(OptionsParser->getCompilations(),
278-
OptionsParser->getSourcePathList())
279-
.run(&Factory) ||
280-
Errors != 0;
293+
return Tool.run(&Factory) || Errors != 0;
281294
}

0 commit comments

Comments
 (0)