// FIXME: Should we try to filter out FixIts that apply to our generated
// code, and not the user's expression?
if (severity == eDiagnosticSeverityError) {
- size_t num_fixit_hints = Info.getNumFixItHints();
- for (size_t i = 0; i < num_fixit_hints; i++) {
- const clang::FixItHint &fixit = Info.getFixItHint(i);
- if (!fixit.isNull())
- new_diagnostic->AddFixitHint(fixit);
+ for (const clang::FixItHint &fixit : Info.getFixItHints()) {
+ if (fixit.isNull())
+ continue;
+ new_diagnostic->AddFixitHint(fixit);
}
}
return abi;
}
+/// Applies the given Fix-It hint to the given commit.
+static void ApplyFixIt(const FixItHint &fixit, clang::edit::Commit &commit) {
+ // This is cobbed from clang::Rewrite::FixItRewriter.
+ if (fixit.CodeToInsert.empty()) {
+ if (fixit.InsertFromRange.isValid()) {
+ commit.insertFromRange(fixit.RemoveRange.getBegin(),
+ fixit.InsertFromRange, /*afterToken=*/false,
+ fixit.BeforePreviousInsertions);
+ return;
+ }
+ commit.remove(fixit.RemoveRange);
+ return;
+ }
+ if (fixit.RemoveRange.isTokenRange() ||
+ fixit.RemoveRange.getBegin() != fixit.RemoveRange.getEnd()) {
+ commit.replace(fixit.RemoveRange, fixit.CodeToInsert);
+ return;
+ }
+ commit.insert(fixit.RemoveRange.getBegin(), fixit.CodeToInsert,
+ /*afterToken=*/false, fixit.BeforePreviousInsertions);
+}
+
bool ClangExpressionParser::RewriteExpression(
DiagnosticManager &diagnostic_manager) {
clang::SourceManager &source_manager = m_compiler->getSourceManager();
for (const auto &diag : diagnostic_manager.Diagnostics()) {
const auto *diagnostic = llvm::dyn_cast<ClangDiagnostic>(diag.get());
- if (diagnostic && diagnostic->HasFixIts()) {
- for (const FixItHint &fixit : diagnostic->FixIts()) {
- // This is cobbed from clang::Rewrite::FixItRewriter.
- if (fixit.CodeToInsert.empty()) {
- if (fixit.InsertFromRange.isValid()) {
- commit.insertFromRange(fixit.RemoveRange.getBegin(),
- fixit.InsertFromRange, /*afterToken=*/false,
- fixit.BeforePreviousInsertions);
- } else
- commit.remove(fixit.RemoveRange);
- } else {
- if (fixit.RemoveRange.isTokenRange() ||
- fixit.RemoveRange.getBegin() != fixit.RemoveRange.getEnd())
- commit.replace(fixit.RemoveRange, fixit.CodeToInsert);
- else
- commit.insert(fixit.RemoveRange.getBegin(), fixit.CodeToInsert,
- /*afterToken=*/false, fixit.BeforePreviousInsertions);
- }
- }
- }
+ if (!diagnostic)
+ continue;
+ if (!diagnostic->HasFixIts())
+ continue;
+ for (const FixItHint &fixit : diagnostic->FixIts())
+ ApplyFixIt(fixit, commit);
}
// FIXME - do we want to try to propagate specific errors here?