[clang-tools-extra] 66945b6 - Add Optional overload to DiagnosticBuilder operator <<
Nathan James via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 10 10:44:16 PDT 2020
Author: Nathan James
Date: 2020-03-10T17:44:10Z
New Revision: 66945b62f42f0911a328a354cbc7db40b05b76eb
URL: https://github.com/llvm/llvm-project/commit/66945b62f42f0911a328a354cbc7db40b05b76eb
DIFF: https://github.com/llvm/llvm-project/commit/66945b62f42f0911a328a354cbc7db40b05b76eb.diff
LOG: 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
Added:
Modified:
clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp
clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp
clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
clang/include/clang/Basic/Diagnostic.h
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp
index 7c8f4d60dc06..3f0ca4adb54c 100644
--- a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp
+++ b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp
@@ -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(
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
index a5d9275afaf7..f616efb1db6b 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
@@ -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;
}
}
}
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp
index d8997f8a7210..56a8a8140271 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp
@@ -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;
}
diff --git a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
index b23ef6dd6b7d..3466a8a17998 100644
--- a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
@@ -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
diff --git a/clang/include/clang/Basic/Diagnostic.h b/clang/include/clang/Basic/Diagnostic.h
index 6bc8a143a26d..c55b18048772 100644
--- a/clang/include/clang/Basic/Diagnostic.h
+++ b/clang/include/clang/Basic/Diagnostic.h
@@ -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>;
More information about the cfe-commits
mailing list