Skip to content

Commit 277eed4

Browse files
committed
C-import: treat import errors as warnings and continue importing
1 parent ef57aab commit 277eed4

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

src/sema/c-import.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ struct CToCxConverter final : clang::ASTConsumer {
249249
}
250250

251251
bool HandleTopLevelDecl(clang::DeclGroupRef declGroup) override {
252-
try {
253-
for (clang::Decl* decl : declGroup) {
252+
for (clang::Decl* decl : declGroup) {
253+
try {
254254
switch (decl->getKind()) {
255255
case clang::Decl::Function: {
256256
auto functionDecl = toCx(*llvm::cast<clang::FunctionDecl>(decl));
@@ -304,15 +304,18 @@ struct CToCxConverter final : clang::ASTConsumer {
304304
default:
305305
break;
306306
}
307-
}
308307

309-
// Can't throw exceptions through the Clang API as it has exceptions disabled,
310-
// so need to handle them here.
311-
} catch (const CompileError& error) {
312-
error.report();
313-
} catch (...) {
314-
llvm_unreachable("Unhandled exception");
308+
// Can't throw exceptions through the Clang API as it has exceptions disabled,
309+
// so need to handle them here.
310+
} catch (const CompileError& error) {
311+
CompileError augmentedError = error;
312+
augmentedError.message.insert(0, "encountered an internal compiler error in C import: ");
313+
augmentedError.reportAsWarning();
314+
} catch (...) {
315+
WARN(Location(), "Unhandled exception in C import");
316+
}
315317
}
318+
316319
return true; // continue parsing
317320
}
318321

src/support/utility.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,21 @@ CompileError::CompileError(Location location, std::string&& message, std::vector
8282
: location(location), message(std::move(message)), notes(std::move(notes)) {}
8383

8484
void CompileError::report() const {
85-
if (message.empty()) return; // This is a "silent" error, resulting from a previous, reported error.
85+
if (message.empty()) return;
8686

8787
StringFormatter s;
8888
s << message;
8989
reportError(location, s, notes);
9090
}
9191

92+
void CompileError::reportAsWarning() const {
93+
if (message.empty()) return;
94+
95+
StringFormatter s;
96+
s << message;
97+
reportWarning(location, s, notes);
98+
}
99+
92100
std::optional<std::string> cx::findExternalCCompiler() {
93101
#ifdef _WIN32
94102
auto compilers = {"cl.exe", "clang-cl.exe"};
@@ -128,12 +136,16 @@ void cx::reportError(Location location, StringFormatter& message, llvm::ArrayRef
128136
}
129137
}
130138

131-
void cx::reportWarning(Location location, StringFormatter& message) {
139+
void cx::reportWarning(Location location, StringFormatter& message, llvm::ArrayRef<Note> notes) {
132140
if (disableWarnings) return;
133141

134142
if (warningsAsErrors) {
135-
reportError(location, message);
143+
reportError(location, message, notes);
136144
} else {
137145
printDiagnostic(location, "warning", llvm::raw_ostream::YELLOW, message.str());
146+
147+
for (auto& note : notes) {
148+
printDiagnostic(note.location, "note", llvm::raw_ostream::BLACK, note.message);
149+
}
138150
}
139151
}

src/support/utility.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ struct CompileError : std::exception {
6060
static CompileError dependentError() { return CompileError(Location(), ""); }
6161
const char* what() const noexcept override { return message.c_str(); }
6262
void report() const;
63+
void reportAsWarning() const;
6364

6465
Location location;
65-
std::string message;
66+
std::string message; // Empty if this is a silent "dependent error", resulting from a previous, reported error.
6667
std::vector<Note> notes;
6768
};
6869

@@ -75,7 +76,7 @@ template<typename T> void printColored(const T& text, llvm::raw_ostream::Colors
7576
void printStackTrace();
7677
[[noreturn]] void abort(StringFormatter& message);
7778
void reportError(Location location, StringFormatter& message, llvm::ArrayRef<Note> notes = {});
78-
void reportWarning(Location location, StringFormatter& message);
79+
void reportWarning(Location location, StringFormatter& message, llvm::ArrayRef<Note> notes = {});
7980

8081
#define ABORT(args) \
8182
{ \

0 commit comments

Comments
 (0)