Add Optional overload to DiagnosticBuilder operator <<

Reviewers: aaron.ballman, gribozavr2, lebedev.ri

Reviewed By: gribozavr2

Subscribers: wuzish, nemanjai, kbarton, arphaman, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D75714
This commit is contained in:
Nathan James 2020-03-10 13:41:01 +00:00
parent d67cf7a0a9
commit 66945b62f4
5 changed files with 33 additions and 19 deletions

View File

@ -105,12 +105,9 @@ void StringFindStartswithCheck::check(const MatchFinder::MatchResult &Result) {
// Create a preprocessor #include FixIt hint (CreateIncludeInsertion checks
// whether this already exists).
auto IncludeHint = IncludeInserter->CreateIncludeInsertion(
Diagnostic << IncludeInserter->CreateIncludeInsertion(
Source.getFileID(ComparisonExpr->getBeginLoc()), AbseilStringsMatchHeader,
false);
if (IncludeHint) {
Diagnostic << *IncludeHint;
}
}
void StringFindStartswithCheck::registerPPCallbacks(

View File

@ -96,10 +96,8 @@ void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) {
MatchedDecl->getName().size()),
InitializationString);
if (AddMathInclude) {
auto IncludeHint = IncludeInserter->CreateIncludeInsertion(
Diagnostic << IncludeInserter->CreateIncludeInsertion(
Source.getFileID(MatchedDecl->getBeginLoc()), MathHeader, false);
if (IncludeHint)
Diagnostic << *IncludeHint;
}
}
}

View File

@ -86,13 +86,10 @@ void ProBoundsConstantArrayIndexCheck::check(
SourceRange(BaseRange.getEnd().getLocWithOffset(1),
IndexRange.getBegin().getLocWithOffset(-1)),
", ")
<< FixItHint::CreateReplacement(Matched->getEndLoc(), ")");
Optional<FixItHint> Insertion = Inserter->CreateIncludeInsertion(
Result.SourceManager->getMainFileID(), GslHeader,
/*IsAngled=*/false);
if (Insertion)
Diag << Insertion.getValue();
<< FixItHint::CreateReplacement(Matched->getEndLoc(), ")")
<< Inserter->CreateIncludeInsertion(
Result.SourceManager->getMainFileID(), GslHeader,
/*IsAngled=*/false);
}
return;
}

View File

@ -429,11 +429,9 @@ void MakeSmartPtrCheck::insertHeader(DiagnosticBuilder &Diag, FileID FD) {
if (MakeSmartPtrFunctionHeader.empty()) {
return;
}
if (auto IncludeFixit = Inserter->CreateIncludeInsertion(
FD, MakeSmartPtrFunctionHeader,
/*IsAngled=*/MakeSmartPtrFunctionHeader == StdMemoryHeader)) {
Diag << *IncludeFixit;
}
Diag << Inserter->CreateIncludeInsertion(
FD, MakeSmartPtrFunctionHeader,
/*IsAngled=*/MakeSmartPtrFunctionHeader == StdMemoryHeader);
}
} // namespace modernize

View File

@ -21,6 +21,7 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator_range.h"
@ -1288,6 +1289,29 @@ inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
return DB;
}
inline const DiagnosticBuilder &
operator<<(const DiagnosticBuilder &DB,
const llvm::Optional<SourceRange> &Opt) {
if (Opt)
DB << *Opt;
return DB;
}
inline const DiagnosticBuilder &
operator<<(const DiagnosticBuilder &DB,
const llvm::Optional<CharSourceRange> &Opt) {
if (Opt)
DB << *Opt;
return DB;
}
inline const DiagnosticBuilder &
operator<<(const DiagnosticBuilder &DB, const llvm::Optional<FixItHint> &Opt) {
if (Opt)
DB << *Opt;
return DB;
}
/// A nullability kind paired with a bit indicating whether it used a
/// context-sensitive keyword.
using DiagNullabilityKind = std::pair<NullabilityKind, bool>;