aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Frontend/ASTUnit.cpp
diff options
context:
space:
mode:
authorLemonBoy <thatlemon@gmail.com>2020-10-22 14:13:07 -0700
committerDavid Blaikie <dblaikie@gmail.com>2020-10-22 14:14:19 -0700
commitfd14a1f6fff37ef62a35ca0f8bc630ac8b23516d (patch)
tree671bcc28529a3c3a1cb8d96ed7868276cd083610 /clang/lib/Frontend/ASTUnit.cpp
parentd098bb39aa615c3d3d97e3977dcc70b9de806a3f (diff)
downloadllvm-fd14a1f6fff37ef62a35ca0f8bc630ac8b23516d.zip
llvm-fd14a1f6fff37ef62a35ca0f8bc630ac8b23516d.tar.gz
llvm-fd14a1f6fff37ef62a35ca0f8bc630ac8b23516d.tar.bz2
[clang][Frontend] Add missing error handling
Some early errors during the ASTUnit creation were not transferred to the `FailedParseDiagnostic` so when the code in `LoadFromCommandLine` swaps its content with the content of `StoredDiagnostics` they cannot be retrieved by the user in any way. Reviewed By: andrewrk, dblaikie Differential Revision: https://reviews.llvm.org/D78658
Diffstat (limited to 'clang/lib/Frontend/ASTUnit.cpp')
-rw-r--r--clang/lib/Frontend/ASTUnit.cpp32
1 files changed, 18 insertions, 14 deletions
diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp
index 7fca190..0728fee 100644
--- a/clang/lib/Frontend/ASTUnit.cpp
+++ b/clang/lib/Frontend/ASTUnit.cpp
@@ -69,6 +69,7 @@
#include "llvm/ADT/None.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
@@ -1118,6 +1119,19 @@ bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
std::unique_ptr<CompilerInstance> Clang(
new CompilerInstance(std::move(PCHContainerOps)));
+ // Clean up on error, disengage it if the function returns successfully.
+ auto CleanOnError = llvm::make_scope_exit([&]() {
+ // Remove the overridden buffer we used for the preamble.
+ SavedMainFileBuffer = nullptr;
+
+ // Keep the ownership of the data in the ASTUnit because the client may
+ // want to see the diagnostics.
+ transferASTDataFromCompilerInstance(*Clang);
+ FailedParseDiagnostics.swap(StoredDiagnostics);
+ StoredDiagnostics.clear();
+ NumStoredDiagnosticsFromDriver = 0;
+ });
+
// Ensure that Clang has a FileManager with the right VFS, which may have
// changed above in AddImplicitPreamble. If VFS is nullptr, rely on
// createFileManager to create one.
@@ -1200,7 +1214,7 @@ bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
ActCleanup(Act.get());
if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
- goto error;
+ return true;
if (SavedMainFileBuffer)
TranslateStoredDiagnostics(getFileManager(), getSourceManager(),
@@ -1210,7 +1224,7 @@ bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
if (llvm::Error Err = Act->Execute()) {
consumeError(std::move(Err)); // FIXME this drops errors on the floor.
- goto error;
+ return true;
}
transferASTDataFromCompilerInstance(*Clang);
@@ -1219,19 +1233,9 @@ bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
FailedParseDiagnostics.clear();
- return false;
+ CleanOnError.release();
-error:
- // Remove the overridden buffer we used for the preamble.
- SavedMainFileBuffer = nullptr;
-
- // Keep the ownership of the data in the ASTUnit because the client may
- // want to see the diagnostics.
- transferASTDataFromCompilerInstance(*Clang);
- FailedParseDiagnostics.swap(StoredDiagnostics);
- StoredDiagnostics.clear();
- NumStoredDiagnosticsFromDriver = 0;
- return true;
+ return false;
}
static std::pair<unsigned, unsigned>