Skip to content

Commit 25e9836

Browse files
committed
Report errors when linking of generated C++ fails
Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
1 parent b2cc4ab commit 25e9836

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

src/CppParser/Link.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
using namespace CppSharp::CppParser;
1515

16-
void Parser::Link(const std::string& File, const CppLinkerOptions* LinkerOptions)
16+
bool Parser::Link(const std::string& File, const CppLinkerOptions* LinkerOptions)
1717
{
1818
std::vector<const char*> args;
1919
llvm::StringRef Dir(llvm::sys::path::parent_path(File));
@@ -27,33 +27,29 @@ void Parser::Link(const std::string& File, const CppLinkerOptions* LinkerOptions
2727
switch (Triple.getEnvironment())
2828
{
2929
case llvm::Triple::EnvironmentType::MSVC:
30-
LinkWindows(LinkerOptions, args, Dir, Stem);
31-
break;
30+
return LinkWindows(LinkerOptions, args, Dir, Stem);
3231

3332
case llvm::Triple::EnvironmentType::GNU:
34-
LinkWindows(LinkerOptions, args, Dir, Stem, true);
35-
break;
33+
return LinkWindows(LinkerOptions, args, Dir, Stem, true);
3634

3735
default:
3836
throw std::invalid_argument("Target triple environment");
3937
}
4038
break;
4139

4240
case llvm::Triple::OSType::Linux:
43-
LinkELF(LinkerOptions, args, Dir, Stem);
44-
break;
41+
return LinkELF(LinkerOptions, args, Dir, Stem);
4542

4643
case llvm::Triple::OSType::Darwin:
4744
case llvm::Triple::OSType::MacOSX:
48-
LinkMachO(LinkerOptions, args, Dir, Stem);
49-
break;
45+
return LinkMachO(LinkerOptions, args, Dir, Stem);
5046

5147
default:
5248
throw std::invalid_argument("Target triple operating system");
5349
}
5450
}
5551

56-
void Parser::LinkWindows(const CppLinkerOptions* LinkerOptions,
52+
bool Parser::LinkWindows(const CppLinkerOptions* LinkerOptions,
5753
std::vector<const char*>& args,
5854
const llvm::StringRef& Dir, llvm::StringRef& Stem, bool MinGW)
5955
{
@@ -104,11 +100,13 @@ void Parser::LinkWindows(const CppLinkerOptions* LinkerOptions,
104100
std::string Out("-out:" + std::string(Output));
105101
args.push_back(Out.data());
106102

107-
lld::coff::link(args, false, outs(), errs());
103+
return lld::coff::link(args, false, outs(), errs());
104+
#else
105+
return false;
108106
#endif
109107
}
110108

111-
void Parser::LinkELF(const CppLinkerOptions* LinkerOptions,
109+
bool Parser::LinkELF(const CppLinkerOptions* LinkerOptions,
112110
std::vector<const char*>& args,
113111
llvm::StringRef& Dir, llvm::StringRef& Stem)
114112
{
@@ -143,11 +141,13 @@ void Parser::LinkELF(const CppLinkerOptions* LinkerOptions,
143141
std::string Out(Output);
144142
args.push_back(Out.data());
145143

146-
lld::elf::link(args, false, outs(), errs());
144+
return lld::elf::link(args, false, outs(), errs());
145+
#else
146+
return false;
147147
#endif
148148
}
149149

150-
void Parser::LinkMachO(const CppLinkerOptions* LinkerOptions,
150+
bool Parser::LinkMachO(const CppLinkerOptions* LinkerOptions,
151151
std::vector<const char*>& args,
152152
llvm::StringRef& Dir, llvm::StringRef& Stem)
153153
{
@@ -182,6 +182,8 @@ void Parser::LinkMachO(const CppLinkerOptions* LinkerOptions,
182182
std::string Out(Output);
183183
args.push_back(Out.data());
184184

185-
lld::macho::link(args, false, outs(), errs());
185+
return lld::macho::link(args, false, outs(), errs());
186+
#else
187+
return false;
186188
#endif
187189
}

src/CppParser/Parser.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4693,13 +4693,20 @@ ParserResult* Parser::Build(const CppLinkerOptions* LinkerOptions, const std::st
46934693
if (error)
46944694
return error;
46954695

4696-
Link(File, LinkerOptions);
4696+
bool LinkingError = !Link(File, LinkerOptions);
46974697

46984698
if (Last)
46994699
llvm::llvm_shutdown();
47004700

47014701
auto res = new ParserResult();
47024702
HandleDiagnostics(res);
4703+
if (LinkingError)
4704+
{
4705+
ParserDiagnostic PD;
4706+
PD.level = ParserDiagnosticLevel::Error;
4707+
PD.lineNumber = PD.columnNumber = -1;
4708+
res->addDiagnostics(PD);
4709+
}
47034710
return res;
47044711
}
47054712

@@ -4769,10 +4776,20 @@ ParserResult* ClangParser::Link(CppParserOptions* Opts,
47694776
return 0;
47704777

47714778
Parser Parser(Opts);
4772-
Parser.Link(File, LinkerOptions);
4779+
bool LinkingError = !Parser.Link(File, LinkerOptions);
47734780

47744781
if (Last)
47754782
llvm::llvm_shutdown();
4783+
4784+
auto res = new ParserResult();
4785+
if (LinkingError)
4786+
{
4787+
ParserDiagnostic PD;
4788+
PD.level = ParserDiagnosticLevel::Error;
4789+
PD.lineNumber = PD.columnNumber = -1;
4790+
res->addDiagnostics(PD);
4791+
}
4792+
return res;
47764793
}
47774794

47784795
ParserResult* Parser::Compile(const std::string& File)

src/CppParser/Parser.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Parser
5858
static ParserResult* ParseLibrary(const CppLinkerOptions* Opts);
5959
ParserResult* Build(const CppLinkerOptions* LinkerOptions, const std::string& File, bool Last);
6060
ParserResult* Compile(const std::string& File);
61-
void Link(const std::string& File, const CppLinkerOptions* LinkerOptions);
61+
bool Link(const std::string& File, const CppLinkerOptions* LinkerOptions);
6262
void WalkAST(clang::TranslationUnitDecl* TU);
6363
void HandleDeclaration(const clang::Decl* D, Declaration* Decl);
6464
CppParserOptions* opts;
@@ -172,11 +172,11 @@ class Parser
172172
llvm::object::ObjectFile* ObjectFile, std::vector<CppSharp::CppParser::NativeLibrary*>& NativeLibs);
173173
ParserTargetInfo* GetTargetInfo();
174174

175-
void LinkWindows(const CppLinkerOptions* LinkerOptions, std::vector<const char*>& args,
175+
bool LinkWindows(const CppLinkerOptions* LinkerOptions, std::vector<const char*>& args,
176176
const llvm::StringRef& Dir, llvm::StringRef& Stem, bool MinGW = false);
177-
void LinkELF(const CppLinkerOptions* LinkerOptions, std::vector<const char*>& args,
177+
bool LinkELF(const CppLinkerOptions* LinkerOptions, std::vector<const char*>& args,
178178
llvm::StringRef& Dir, llvm::StringRef& Stem);
179-
void LinkMachO(const CppLinkerOptions* LinkerOptions, std::vector<const char*>& args,
179+
bool LinkMachO(const CppLinkerOptions* LinkerOptions, std::vector<const char*>& args,
180180
llvm::StringRef& Dir, llvm::StringRef& Stem);
181181

182182
int index;

0 commit comments

Comments
 (0)