Skip to content

Commit b393d6a

Browse files
committed
Adding error checking to computeDependencies and remove unused string
buffer.
1 parent 9691b5b commit b393d6a

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "clang/Driver/Driver.h"
1313
#include "clang/Frontend/FrontendActions.h"
1414
#include "clang/Tooling/DependencyScanning/DependencyScanningWorker.h"
15+
#include "llvm/ADT/ScopeExit.h"
1516
#include "llvm/TargetParser/Host.h"
1617

1718
using namespace clang;
@@ -704,9 +705,6 @@ bool DependencyScanningAction::runInvocation(
704705
return Result;
705706
}
706707

707-
const std::string CompilerInstanceWithContext::FakeFileBuffer =
708-
std::string(MAX_NUM_NAMES, ' ');
709-
710708
llvm::Error CompilerInstanceWithContext::initialize() {
711709
std::tie(OverlayFS, CommandLine) = initVFSForByNameScanning(
712710
Worker.BaseFS, CommandLine, CWD, "ScanningByName");
@@ -792,6 +790,17 @@ llvm::Error CompilerInstanceWithContext::computeDependencies(
792790
DependencyActionController &Controller) {
793791
auto &CI = *CIPtr;
794792

793+
// We create this cleanup object because computeDependencies may exit
794+
// early with errors.
795+
auto CleanUp = llvm::make_scope_exit([&]() {
796+
CI.clearDependencyCollectors();
797+
// The preprocessor may not be created at the entry of this method,
798+
// but it must have been created when this method returns, whether
799+
// there are errors during scanning or not.
800+
CI.getPreprocessor().removePPCallbacks();
801+
});
802+
803+
CI.clearDependencyCollectors();
795804
auto MDC = initializeScanInstanceDependencyCollector(
796805
CI, std::make_unique<DependencyOutputOptions>(*OutputOpts), CWD, Consumer,
797806
Worker.Service,
@@ -806,7 +815,9 @@ llvm::Error CompilerInstanceWithContext::computeDependencies(
806815
std::unique_ptr<FrontendAction> Action =
807816
std::make_unique<PreprocessOnlyAction>();
808817
auto InputFile = CI.getFrontendOpts().Inputs.begin();
809-
Action->BeginSourceFile(CI, *InputFile);
818+
bool ActionBeginSucceeded = Action->BeginSourceFile(CI, *InputFile);
819+
assert(ActionBeginSucceeded && "Action BeginSourceFile must succeed");
820+
(void)ActionBeginSucceeded;
810821
}
811822

812823
Preprocessor &PP = CI.getPreprocessor();
@@ -818,7 +829,9 @@ llvm::Error CompilerInstanceWithContext::computeDependencies(
818829
if (!SrcLocOffset) {
819830
// We need to call EnterSourceFile when SrcLocOffset is zero to initialize
820831
// the preprocessor.
821-
PP.EnterSourceFile(MainFileID, nullptr, SourceLocation());
832+
bool PPFailed = PP.EnterSourceFile(MainFileID, nullptr, SourceLocation());
833+
assert(!PPFailed && "Preprocess must be able to enter the main file.");
834+
(void)PPFailed;
822835
CB = MDC->getPPCallbacks();
823836
} else {
824837
// When SrcLocOffset is non-zero, the preprocessor has already been
@@ -847,15 +860,15 @@ llvm::Error CompilerInstanceWithContext::computeDependencies(
847860
// It does not indicate the end of processing the fake file.
848861
CB->EndOfMainFile();
849862

863+
if (!ModResult)
864+
return llvm::make_error<llvm::StringError>(
865+
DiagPrinterWithOS->DiagnosticsOS.str(), llvm::inconvertibleErrorCode());
866+
850867
CompilerInvocation ModuleInvocation(*OriginalInvocation);
851868
MDC->applyDiscoveredDependencies(ModuleInvocation);
852869
Consumer.handleBuildCommand(
853870
{CommandLine[0], ModuleInvocation.getCC1CommandLine()});
854871

855-
// Remove the DependencyCollecgtors and PPCallbacks since they are going out
856-
// of scope.
857-
CI.clearDependencyCollectors();
858-
CI.getPreprocessor().removePPCallbacks();
859872
return llvm::Error::success();
860873
}
861874

clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,6 @@ class CompilerInstanceWithContext {
157157
DependencyScanningWorker &Worker;
158158
llvm::StringRef CWD;
159159
std::vector<std::string> CommandLine;
160-
static const uint64_t MAX_NUM_NAMES = (1 << 12);
161-
static const std::string FakeFileBuffer;
162160

163161
// Context - file systems
164162
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS;

0 commit comments

Comments
 (0)