Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 703ba99

Browse files
committed
IRGen: Factor out function CodeGenAction::loadModule. NFCI.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@292972 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 88a607f commit 703ba99

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

include/clang/CodeGen/CodeGenAction.h

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class CodeGenAction : public ASTFrontendAction {
3131
llvm::LLVMContext *VMContext;
3232
bool OwnsVMContext;
3333

34+
std::unique_ptr<llvm::Module> loadModule(llvm::MemoryBufferRef MBRef);
35+
3436
protected:
3537
/// Create a new code generation action. If the optional \p _VMContext
3638
/// parameter is supplied, the action uses it without taking ownership,

lib/CodeGen/CodeGenAction.cpp

+38-28
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,41 @@ static void BitcodeInlineAsmDiagHandler(const llvm::SMDiagnostic &SM,
838838
Diags->Report(DiagID).AddString("cannot compile inline asm");
839839
}
840840

841+
std::unique_ptr<llvm::Module> CodeGenAction::loadModule(MemoryBufferRef MBRef) {
842+
CompilerInstance &CI = getCompilerInstance();
843+
SourceManager &SM = CI.getSourceManager();
844+
845+
// For ThinLTO backend invocations, ensure that the context
846+
// merges types based on ODR identifiers.
847+
if (!CI.getCodeGenOpts().ThinLTOIndexFile.empty())
848+
VMContext->enableDebugTypeODRUniquing();
849+
850+
llvm::SMDiagnostic Err;
851+
if (std::unique_ptr<llvm::Module> M = parseIR(MBRef, Err, *VMContext))
852+
return M;
853+
854+
// Translate from the diagnostic info to the SourceManager location if
855+
// available.
856+
// TODO: Unify this with ConvertBackendLocation()
857+
SourceLocation Loc;
858+
if (Err.getLineNo() > 0) {
859+
assert(Err.getColumnNo() >= 0);
860+
Loc = SM.translateFileLineCol(SM.getFileEntryForID(SM.getMainFileID()),
861+
Err.getLineNo(), Err.getColumnNo() + 1);
862+
}
863+
864+
// Strip off a leading diagnostic code if there is one.
865+
StringRef Msg = Err.getMessage();
866+
if (Msg.startswith("error: "))
867+
Msg = Msg.substr(7);
868+
869+
unsigned DiagID =
870+
CI.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, "%0");
871+
872+
CI.getDiagnostics().Report(Loc, DiagID) << Msg;
873+
return {};
874+
}
875+
841876
void CodeGenAction::ExecuteAction() {
842877
// If this is an IR file, we have to treat it specially.
843878
if (getCurrentFileKind() == IK_LLVM_IR) {
@@ -855,35 +890,10 @@ void CodeGenAction::ExecuteAction() {
855890
if (Invalid)
856891
return;
857892

858-
// For ThinLTO backend invocations, ensure that the context
859-
// merges types based on ODR identifiers.
860-
if (!CI.getCodeGenOpts().ThinLTOIndexFile.empty())
861-
VMContext->enableDebugTypeODRUniquing();
862-
863-
llvm::SMDiagnostic Err;
864-
TheModule = parseIR(MainFile->getMemBufferRef(), Err, *VMContext);
865-
if (!TheModule) {
866-
// Translate from the diagnostic info to the SourceManager location if
867-
// available.
868-
// TODO: Unify this with ConvertBackendLocation()
869-
SourceLocation Loc;
870-
if (Err.getLineNo() > 0) {
871-
assert(Err.getColumnNo() >= 0);
872-
Loc = SM.translateFileLineCol(SM.getFileEntryForID(FID),
873-
Err.getLineNo(), Err.getColumnNo() + 1);
874-
}
875-
876-
// Strip off a leading diagnostic code if there is one.
877-
StringRef Msg = Err.getMessage();
878-
if (Msg.startswith("error: "))
879-
Msg = Msg.substr(7);
880-
881-
unsigned DiagID =
882-
CI.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, "%0");
883-
884-
CI.getDiagnostics().Report(Loc, DiagID) << Msg;
893+
TheModule = loadModule(*MainFile);
894+
if (!TheModule)
885895
return;
886-
}
896+
887897
const TargetOptions &TargetOpts = CI.getTargetOpts();
888898
if (TheModule->getTargetTriple() != TargetOpts.Triple) {
889899
CI.getDiagnostics().Report(SourceLocation(),

0 commit comments

Comments
 (0)