Skip to content

Commit fadf560

Browse files
committed
add DiagnosticsEngineRAII
1 parent 4e3e4c2 commit fadf560

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

lib/CppInterOp/Compatibility.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,24 @@ class SynthesizingCodeRAII {
430430
SynthesizingCodeRAII(Interpreter* i) : m_Interpreter(i) {}
431431
// ~SynthesizingCodeRAII() {} // TODO: implement
432432
};
433+
434+
class DiagnosticsEngineRAII {
435+
private:
436+
clang::DiagnosticsEngine& diags;
437+
438+
public:
439+
bool reset_condition; // additional condition to reset diagnostics
440+
441+
DiagnosticsEngineRAII(clang::DiagnosticsEngine& d, bool c = true) : diags(d), reset_condition(c) {}
442+
~DiagnosticsEngineRAII() {
443+
if (diags.hasErrorOccurred() && reset_condition) {
444+
// instantiation failed, need to reset DiagnosticsEngine
445+
diags.Reset(/*soft=*/true);
446+
diags.getClient()->clear();
447+
}
448+
}
449+
};
450+
433451
} // namespace compat
434452

435453
#endif // CPPINTEROP_USE_REPL

lib/CppInterOp/CppInterOp.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,7 @@ static void InstantiateFunctionDefinition(Decl* D) {
312312
getSema().InstantiateFunctionDefinition(SourceLocation(), FD,
313313
/*Recursive=*/true,
314314
/*DefinitionRequired=*/true);
315-
// FIXME: this can go into a RAII object
316-
clang::DiagnosticsEngine& Diags = getSema().getDiagnostics();
317-
if (!FD->isDefined() && Diags.hasErrorOccurred()) {
318-
// instantiation failed, need to reset DiagnosticsEngine
319-
Diags.Reset(/*soft=*/true);
320-
Diags.getClient()->clear();
321-
}
315+
compat::DiagnosticsEngineRAII diagsRAII(getSema().getDiagnostics(), !FD->isDefined());
322316
}
323317
}
324318

0 commit comments

Comments
 (0)