[clang-tools-extra] [clang] [clang] Extend diagnose_if to accept more detailed warning information (PR #70976)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 1 13:00:26 PDT 2023


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff 86f2e0925051a860eb0a2c187b185b47c88ec45a e0389b126920ed6dd89d729908e6c39b313da65f -- clang/test/SemaCXX/diagnose_if-warning-group.cpp clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h clang-tools-extra/clangd/Diagnostics.cpp clang-tools-extra/clangd/ParsedAST.cpp clang/include/clang/Basic/Diagnostic.h clang/include/clang/Basic/DiagnosticCategories.h clang/include/clang/Basic/DiagnosticIDs.h clang/lib/Basic/Diagnostic.cpp clang/lib/Basic/DiagnosticIDs.cpp clang/lib/Frontend/LogDiagnosticPrinter.cpp clang/lib/Frontend/SerializedDiagnosticPrinter.cpp clang/lib/Frontend/TextDiagnosticPrinter.cpp clang/lib/Sema/Sema.cpp clang/lib/Sema/SemaCUDA.cpp clang/lib/Sema/SemaDeclAttr.cpp clang/lib/Sema/SemaOverload.cpp clang/lib/Sema/SemaTemplateInstantiateDecl.cpp clang/lib/Serialization/ASTReader.cpp clang/lib/Serialization/ASTWriter.cpp clang/tools/diagtool/ListWarnings.cpp clang/tools/diagtool/ShowEnabledWarnings.cpp clang/tools/libclang/CXStoredDiagnostic.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
index a2f78d8f060a..b19a84f5dc21 100644
--- a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
+++ b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
@@ -177,8 +177,8 @@ DiagnosticBuilder ClangTidyContext::diag(
     StringRef CheckName, SourceLocation Loc, StringRef Description,
     DiagnosticIDs::Level Level /* = DiagnosticIDs::Warning*/) {
   assert(Loc.isValid());
-  unsigned ID = DiagEngine->getDiagnosticIDs()->getCustomDiagID(Level,
-      (Description + " [" + CheckName + "]").str());
+  unsigned ID = DiagEngine->getDiagnosticIDs()->getCustomDiagID(
+      Level, (Description + " [" + CheckName + "]").str());
   CheckNamesByDiagnosticID.try_emplace(ID, CheckName);
   return DiagEngine->Report(Loc, ID);
 }
@@ -186,8 +186,8 @@ DiagnosticBuilder ClangTidyContext::diag(
 DiagnosticBuilder ClangTidyContext::diag(
     StringRef CheckName, StringRef Description,
     DiagnosticIDs::Level Level /* = DiagnosticIDs::Warning*/) {
-  unsigned ID = DiagEngine->getDiagnosticIDs()->getCustomDiagID(Level,
-      (Description + " [" + CheckName + "]").str());
+  unsigned ID = DiagEngine->getDiagnosticIDs()->getCustomDiagID(
+      Level, (Description + " [" + CheckName + "]").str());
   CheckNamesByDiagnosticID.try_emplace(ID, CheckName);
   return DiagEngine->Report(ID);
 }
diff --git a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h
index c7694ad05f03..6a4a5175c3d5 100644
--- a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h
+++ b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h
@@ -79,9 +79,7 @@ public:
     this->DiagEngine = DiagEngine;
   }
 
-  const DiagnosticsEngine* getDiagnosticsEngine() const {
-    return DiagEngine;
-  }
+  const DiagnosticsEngine *getDiagnosticsEngine() const { return DiagEngine; }
 
   ~ClangTidyContext();
 
diff --git a/clang/include/clang/Basic/Diagnostic.h b/clang/include/clang/Basic/Diagnostic.h
index 168e403a798a..547a969ef348 100644
--- a/clang/include/clang/Basic/Diagnostic.h
+++ b/clang/include/clang/Basic/Diagnostic.h
@@ -331,7 +331,7 @@ private:
     // Map extensions to warnings or errors?
     diag::Severity ExtBehavior = diag::Severity::Ignored;
 
-    DiagnosticIDs& DiagIDs;
+    DiagnosticIDs &DiagIDs;
 
     DiagState(DiagnosticIDs &DiagIDs)
         : IgnoreAllWarnings(false), EnableAllWarnings(false),
diff --git a/clang/include/clang/Basic/DiagnosticCategories.h b/clang/include/clang/Basic/DiagnosticCategories.h
index e9a1fe87202d..839f8dee3ca8 100644
--- a/clang/include/clang/Basic/DiagnosticCategories.h
+++ b/clang/include/clang/Basic/DiagnosticCategories.h
@@ -21,8 +21,8 @@ namespace clang {
     };
 
     enum class Group {
-#define DIAG_ENTRY(GroupName, FlagNameOffset, Members, SubGroups, Docs)        \
-  GroupName,
+#define DIAG_ENTRY(GroupName, FlagNameOffset, Members, SubGroups, Docs)    \
+      GroupName,
 #include "clang/Basic/DiagnosticGroups.inc"
 #undef CATEGORY
 #undef DIAG_ENTRY
diff --git a/clang/include/clang/Basic/DiagnosticIDs.h b/clang/include/clang/Basic/DiagnosticIDs.h
index b8aedc732fc3..42a3f9c145a3 100644
--- a/clang/include/clang/Basic/DiagnosticIDs.h
+++ b/clang/include/clang/Basic/DiagnosticIDs.h
@@ -172,17 +172,15 @@ public:
 class DiagnosticIDs : public RefCountedBase<DiagnosticIDs> {
 public:
   /// The level of the diagnostic, after it has been through mapping.
-  enum Level : uint8_t {
-    Ignored, Note, Remark, Warning, Error, Fatal
-  };
+  enum Level : uint8_t { Ignored, Note, Remark, Warning, Error, Fatal };
 
   // Diagnostic classes.
   enum Class {
-    CLASS_NOTE       = 0x01,
-    CLASS_REMARK     = 0x02,
-    CLASS_WARNING    = 0x03,
-    CLASS_EXTENSION  = 0x04,
-    CLASS_ERROR      = 0x05
+    CLASS_NOTE = 0x01,
+    CLASS_REMARK = 0x02,
+    CLASS_WARNING = 0x03,
+    CLASS_EXTENSION = 0x04,
+    CLASS_ERROR = 0x05
   };
 
   struct CustomDiagDesc {
@@ -194,17 +192,19 @@ public:
     diag::Group Group = {};
     std::string Description;
 
-    friend bool operator==(const CustomDiagDesc &lhs, const CustomDiagDesc &rhs) {
+    friend bool operator==(const CustomDiagDesc &lhs,
+                           const CustomDiagDesc &rhs) {
       return lhs.DefaultSeverity == rhs.DefaultSeverity &&
-              lhs.Class == rhs.Class &&
-              lhs.ShowInSystemHeader == rhs.ShowInSystemHeader &&
-              lhs.ShowInSystemMacro == rhs.ShowInSystemMacro &&
-              lhs.HasGroup == rhs.HasGroup &&
-              (!lhs.HasGroup || lhs.Group == rhs.Group) &&
-              lhs.Description == rhs.Description;
+             lhs.Class == rhs.Class &&
+             lhs.ShowInSystemHeader == rhs.ShowInSystemHeader &&
+             lhs.ShowInSystemMacro == rhs.ShowInSystemMacro &&
+             lhs.HasGroup == rhs.HasGroup &&
+             (!lhs.HasGroup || lhs.Group == rhs.Group) &&
+             lhs.Description == rhs.Description;
     }
 
-    friend bool operator<(const CustomDiagDesc& lhs, const CustomDiagDesc& rhs) {
+    friend bool operator<(const CustomDiagDesc &lhs,
+                          const CustomDiagDesc &rhs) {
       if (lhs.DefaultSeverity != rhs.DefaultSeverity)
         return lhs.DefaultSeverity < rhs.DefaultSeverity;
       if (lhs.Class != rhs.Class)
@@ -266,7 +266,7 @@ public:
                 .Class = CLASS_ERROR,
                 .Description = std::string(Message)};
       case DiagnosticIDs::Level::Fatal:
-        return {.DefaultSeverity=diag::Severity::Fatal,
+        return {.DefaultSeverity = diag::Severity::Fatal,
                 .Class = CLASS_ERROR,
                 .Description = std::string(Message)};
       }
diff --git a/clang/lib/Basic/DiagnosticIDs.cpp b/clang/lib/Basic/DiagnosticIDs.cpp
index 6b2ba485533f..e05d948681ca 100644
--- a/clang/lib/Basic/DiagnosticIDs.cpp
+++ b/clang/lib/Basic/DiagnosticIDs.cpp
@@ -255,59 +255,58 @@ CATEGORY(REFACTORING, ANALYSIS)
   return Found;
 }
 
-
 //===----------------------------------------------------------------------===//
 // Custom Diagnostic information
 //===----------------------------------------------------------------------===//
 
 namespace clang {
-  namespace diag {
-    using CustomDiagDesc = DiagnosticIDs::CustomDiagDesc;
-    class CustomDiagInfo {
-      std::vector<CustomDiagDesc> DiagInfo;
-      std::map<CustomDiagDesc, unsigned> DiagIDs;
-      std::map<diag::Group, std::vector<unsigned>> GroupToDiags;
-    public:
-
-      /// getDescription - Return the description of the specified custom
-      /// diagnostic.
-      const CustomDiagDesc& getDescription(unsigned DiagID) const {
-        assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() &&
-               "Invalid diagnostic ID");
-        return DiagInfo[DiagID-DIAG_UPPER_LIMIT];
-      }
-
-      unsigned getOrCreateDiagID(DiagnosticIDs::CustomDiagDesc D) {
-        // Check to see if it already exists.
-        std::map<CustomDiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
-        if (I != DiagIDs.end() && I->first == D)
-          return I->second;
-
-        // If not, assign a new ID.
-        unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
-        DiagIDs.insert(std::make_pair(D, ID));
-        DiagInfo.push_back(D);
-        if (D.HasGroup)
-          GroupToDiags[D.Group].emplace_back(ID);
-        return ID;
-      }
-
-      ArrayRef<unsigned> getDiagsInGroup(diag::Group G) const {
-        if (auto Diags = GroupToDiags.find(G); Diags != GroupToDiags.end())
-          return Diags->second;
-        return {};
-      }
-    };
-
-  } // end diag namespace
-} // end clang namespace
+namespace diag {
+using CustomDiagDesc = DiagnosticIDs::CustomDiagDesc;
+class CustomDiagInfo {
+  std::vector<CustomDiagDesc> DiagInfo;
+  std::map<CustomDiagDesc, unsigned> DiagIDs;
+  std::map<diag::Group, std::vector<unsigned>> GroupToDiags;
+
+public:
+  /// getDescription - Return the description of the specified custom
+  /// diagnostic.
+  const CustomDiagDesc &getDescription(unsigned DiagID) const {
+    assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() &&
+           "Invalid diagnostic ID");
+    return DiagInfo[DiagID - DIAG_UPPER_LIMIT];
+  }
+
+  unsigned getOrCreateDiagID(DiagnosticIDs::CustomDiagDesc D) {
+    // Check to see if it already exists.
+    std::map<CustomDiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
+    if (I != DiagIDs.end() && I->first == D)
+      return I->second;
+
+    // If not, assign a new ID.
+    unsigned ID = DiagInfo.size() + DIAG_UPPER_LIMIT;
+    DiagIDs.insert(std::make_pair(D, ID));
+    DiagInfo.push_back(D);
+    if (D.HasGroup)
+      GroupToDiags[D.Group].emplace_back(ID);
+    return ID;
+  }
+
+  ArrayRef<unsigned> getDiagsInGroup(diag::Group G) const {
+    if (auto Diags = GroupToDiags.find(G); Diags != GroupToDiags.end())
+      return Diags->second;
+    return {};
+  }
+};
+
+} // namespace diag
+} // namespace clang
 
 DiagnosticMapping DiagnosticIDs::getDefaultMapping(unsigned DiagID) const {
   DiagnosticMapping Info = DiagnosticMapping::Make(
       diag::Severity::Fatal, /*IsUser=*/false, /*IsPragma=*/false);
 
   if (DiagID >= diag::DIAG_UPPER_LIMIT) {
-    const auto& Diag = CustomDiagInfo->getDescription(DiagID);
+    const auto &Diag = CustomDiagInfo->getDescription(DiagID);
     if (auto GroupSev = GroupSeverity[static_cast<size_t>(Diag.Group)];
         GroupSev == diag::Severity())
       Info.setSeverity(Diag.DefaultSeverity);
@@ -398,8 +397,7 @@ DiagnosticIDs::~DiagnosticIDs() {}
 ///
 /// \param FormatString A fixed diagnostic format string that will be hashed and
 /// mapped to a unique DiagID.
-unsigned DiagnosticIDs::getCustomDiagID(
-    CustomDiagDesc Diag) {
+unsigned DiagnosticIDs::getCustomDiagID(CustomDiagDesc Diag) {
   if (!CustomDiagInfo)
     CustomDiagInfo.reset(new diag::CustomDiagInfo());
   return CustomDiagInfo->getOrCreateDiagID(Diag);
@@ -414,8 +412,7 @@ bool DiagnosticIDs::isWarningOrExtension(unsigned DiagID) const {
 /// Determine whether the given built-in diagnostic ID is a
 /// Note.
 bool DiagnosticIDs::isNote(unsigned DiagID) const {
-  return DiagID < diag::DIAG_UPPER_LIMIT &&
-    getDiagClass(DiagID) == CLASS_NOTE;
+  return DiagID < diag::DIAG_UPPER_LIMIT && getDiagClass(DiagID) == CLASS_NOTE;
 }
 
 /// isExtensionDiag - Determine whether the given built-in diagnostic
@@ -423,7 +420,8 @@ bool DiagnosticIDs::isNote(unsigned DiagID) const {
 /// which is set to indicate whether the diagnostic is ignored by default (in
 /// which case -pedantic enables it) or treated as a warning/error by default.
 ///
-bool DiagnosticIDs::isExtensionDiag(unsigned DiagID, bool &EnabledByDefault) const {
+bool DiagnosticIDs::isExtensionDiag(unsigned DiagID,
+                                    bool &EnabledByDefault) const {
   if (DiagID >= diag::DIAG_UPPER_LIMIT ||
       getDiagClass(DiagID) != CLASS_EXTENSION)
     return false;
@@ -700,7 +698,8 @@ DiagnosticIDs::getDiagnosticsInGroup(diag::Flavor Flavor, StringRef Group,
                                      SmallVectorImpl<diag::kind> &Diags) const {
   if (std::optional<diag::Group> G = getGroupForWarningOption(Group)) {
     if (CustomDiagInfo)
-      llvm::copy(CustomDiagInfo->getDiagsInGroup(*G), std::back_inserter(Diags));
+      llvm::copy(CustomDiagInfo->getDiagsInGroup(*G),
+                 std::back_inserter(Diags));
     return ::getDiagnosticsInGroup(
         Flavor, &OptionTable[static_cast<unsigned>(*G)], Diags);
   }
diff --git a/clang/lib/Frontend/TextDiagnosticPrinter.cpp b/clang/lib/Frontend/TextDiagnosticPrinter.cpp
index 5e748c620161..fa2042ac8a69 100644
--- a/clang/lib/Frontend/TextDiagnosticPrinter.cpp
+++ b/clang/lib/Frontend/TextDiagnosticPrinter.cpp
@@ -70,8 +70,10 @@ static void printDiagnosticOptions(raw_ostream &OS,
     // flag it as such. Note that diagnostics could also have been mapped by a
     // pragma, but we don't currently have a way to distinguish this.
     if (Level == DiagnosticsEngine::Error &&
-        Info.getDiags()->getDiagnosticIDs()->isWarningOrExtension(Info.getID()) &&
-        !Info.getDiags()->getDiagnosticIDs()->isDefaultMappingAsError(Info.getID())) {
+        Info.getDiags()->getDiagnosticIDs()->isWarningOrExtension(
+            Info.getID()) &&
+        !Info.getDiags()->getDiagnosticIDs()->isDefaultMappingAsError(
+            Info.getID())) {
       OS << " [-Werror";
       Started = true;
     }
diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index 6365faff00dd..a628c10c7fdd 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -1637,7 +1637,8 @@ bool Sema::hasUncompilableErrorOccurred() const {
   if (Loc == DeviceDeferredDiags.end())
     return false;
   for (auto PDAt : Loc->second) {
-    if (Diags.getDiagnosticIDs()->isDefaultMappingAsError(PDAt.second.getDiagID()))
+    if (Diags.getDiagnosticIDs()->isDefaultMappingAsError(
+            PDAt.second.getDiagID()))
       return true;
   }
   return false;
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index ea7dacd2b05a..f9b05d066505 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -1128,8 +1128,10 @@ static void handleDiagnoseIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
   if (AL.getNumArgs() > 3) {
     if (!S.checkStringLiteralArgumentAttr(AL, 3, WarningGroup))
       return;
-    if (!S.getDiagnostics().getDiagnosticIDs()->getGroupForWarningOption(WarningGroup)) {
-      S.Diag(AL.getArgAsExpr(3)->getBeginLoc(), diag::err_diagnose_if_unknown_warning);
+    if (!S.getDiagnostics().getDiagnosticIDs()->getGroupForWarningOption(
+            WarningGroup)) {
+      S.Diag(AL.getArgAsExpr(3)->getBeginLoc(),
+             diag::err_diagnose_if_unknown_warning);
       return;
     }
   }
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index e79ec8a657f8..a4879de9d67a 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -7179,7 +7179,7 @@ static bool diagnoseDiagnoseIfAttrsWith(Sema &S, const NamedDecl *ND,
       } else {
         DiagnosticIDs::CustomDiagDesc Diag;
         auto DiagGroup = S.Diags.getDiagnosticIDs()->getGroupForWarningOption(
-                      DIA->getWarningGroup());
+            DIA->getWarningGroup());
         assert(DiagGroup);
         Diag.HasGroup = true;
         Diag.Group = *DiagGroup;

``````````

</details>


https://github.com/llvm/llvm-project/pull/70976


More information about the cfe-commits mailing list