Skip to content

[Clang] [NFC] Tablegen component diags headers #134777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 8, 2025

Conversation

Sirraide
Copy link
Member

@Sirraide Sirraide commented Apr 8, 2025

The component diagnostic headers (i.e. DiagnosticAST.h and friends) all follow the same format, and there’s enough of them (and in them) to where updating all of them has become rather tedious (at least it was for me while working on #132348), so this patch instead generates all of them (or rather their contents) via Tablegen.

Also, it seems that %enum_select currently wouldn’t work in DiagnosticCommonKinds.td because the infrastructure for that was missing from DiagnosticIDs.h; this patch should fix that as well.

@Sirraide Sirraide added the clang Clang issues not falling into any other category label Apr 8, 2025
@llvmbot llvmbot added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Apr 8, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 8, 2025

@llvm/pr-subscribers-clang

Author: None (Sirraide)

Changes

The component diagnostic headers (i.e. DiagnosticAST.h and friends) all follow the same format, and there’s enough of them (and in them) to where updating all of them has become rather tedious (at least it was for me while working on #132348), so this patch instead generates all of them (or rather their contents) via Tablegen.

Also, it seems that %enum_select currently woudln’t work in DiagnosticCommonKinds.td because the infrastructure for that was missing from DiagnosticIDs.h; this patch should fix that as well.


Patch is 29.50 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/134777.diff

17 Files Affected:

  • (modified) clang/include/clang/Basic/CMakeLists.txt (+5)
  • (modified) clang/include/clang/Basic/DiagnosticAST.h (+1-39)
  • (modified) clang/include/clang/Basic/DiagnosticAnalysis.h (+1-38)
  • (modified) clang/include/clang/Basic/DiagnosticComment.h (+1-39)
  • (modified) clang/include/clang/Basic/DiagnosticCrossTU.h (+1-39)
  • (modified) clang/include/clang/Basic/DiagnosticDriver.h (+1-39)
  • (modified) clang/include/clang/Basic/DiagnosticFrontend.h (+1-39)
  • (modified) clang/include/clang/Basic/DiagnosticIDs.h (+5-23)
  • (modified) clang/include/clang/Basic/DiagnosticInstallAPI.h (+1-37)
  • (modified) clang/include/clang/Basic/DiagnosticLex.h (+1-38)
  • (modified) clang/include/clang/Basic/DiagnosticParse.h (+1-39)
  • (modified) clang/include/clang/Basic/DiagnosticRefactoring.h (+1-39)
  • (modified) clang/include/clang/Basic/DiagnosticSema.h (+1-40)
  • (modified) clang/include/clang/Basic/DiagnosticSerialization.h (+1-39)
  • (modified) clang/utils/TableGen/ClangDiagnosticsEmitter.cpp (+51)
  • (modified) clang/utils/TableGen/TableGen.cpp (+6)
  • (modified) clang/utils/TableGen/TableGenBackends.h (+2)
diff --git a/clang/include/clang/Basic/CMakeLists.txt b/clang/include/clang/Basic/CMakeLists.txt
index 4d5e1eaa3facb..265ea1fc06494 100644
--- a/clang/include/clang/Basic/CMakeLists.txt
+++ b/clang/include/clang/Basic/CMakeLists.txt
@@ -13,6 +13,11 @@ macro(clang_diag_gen component)
     -gen-clang-diags-compat-ids -clang-component=${component}
     SOURCE Diagnostic.td
     TARGET ClangDiagnostic${component}CompatIDs)
+
+  clang_tablegen(Diagnostic${component}Interface.inc
+    -gen-clang-diags-iface -clang-component=${component}
+    SOURCE Diagnostic.td
+    TARGET ClangDiagnostic${component}Interface)
 endmacro(clang_diag_gen)
 
 clang_diag_gen(Analysis)
diff --git a/clang/include/clang/Basic/DiagnosticAST.h b/clang/include/clang/Basic/DiagnosticAST.h
index 41e2598f7cc3b..be9e303d92629 100644
--- a/clang/include/clang/Basic/DiagnosticAST.h
+++ b/clang/include/clang/Basic/DiagnosticAST.h
@@ -10,44 +10,6 @@
 #define LLVM_CLANG_BASIC_DIAGNOSTICAST_H
 
 #include "clang/Basic/Diagnostic.h"
-
-namespace clang {
-namespace diag {
-enum {
-#define DIAG(ENUM, FLAGS, DEFAULT_MAPPING, DESC, GROUP, SFINAE, NOWERROR,      \
-             SHOWINSYSHEADER, SHOWINSYSMACRO, DEFERRABLE, CATEGORY)            \
-  ENUM,
-#define ASTSTART
-#include "clang/Basic/DiagnosticASTKinds.inc"
-#undef DIAG
-  NUM_BUILTIN_AST_DIAGNOSTICS
-};
-
-#define DIAG_ENUM(ENUM_NAME)                                                   \
-  namespace ENUM_NAME {                                                        \
-  enum {
-#define DIAG_ENUM_ITEM(IDX, NAME) NAME = IDX,
-#define DIAG_ENUM_END()                                                        \
-  }                                                                            \
-  ;                                                                            \
-  }
-#include "clang/Basic/DiagnosticASTEnums.inc"
-#undef DIAG_ENUM_END
-#undef DIAG_ENUM_ITEM
-#undef DIAG_ENUM
-} // end namespace diag
-
-namespace diag_compat {
-#define DIAG_COMPAT_IDS_BEGIN() enum {
-#define DIAG_COMPAT_IDS_END()                                                  \
-  }                                                                            \
-  ;
-#define DIAG_COMPAT_ID(IDX, NAME, ...) NAME = IDX,
-#include "clang/Basic/DiagnosticASTCompatIDs.inc"
-#undef DIAG_COMPAT_ID
-#undef DIAG_COMPAT_IDS_BEGIN
-#undef DIAG_COMPAT_IDS_END
-} // end namespace diag_compat
-} // end namespace clang
+#include "clang/Basic/DiagnosticASTInterface.inc"
 
 #endif // LLVM_CLANG_BASIC_DIAGNOSTICAST_H
diff --git a/clang/include/clang/Basic/DiagnosticAnalysis.h b/clang/include/clang/Basic/DiagnosticAnalysis.h
index 5ead092b946c5..8e2635ffbd78d 100644
--- a/clang/include/clang/Basic/DiagnosticAnalysis.h
+++ b/clang/include/clang/Basic/DiagnosticAnalysis.h
@@ -10,43 +10,6 @@
 #define LLVM_CLANG_BASIC_DIAGNOSTICANALYSIS_H
 
 #include "clang/Basic/Diagnostic.h"
-
-namespace clang {
-namespace diag {
-enum {
-#define DIAG(ENUM, FLAGS, DEFAULT_MAPPING, DESC, GROUP, SFINAE, NOWERROR,      \
-             SHOWINSYSHEADER, SHOWINSYSMACRO, DEFERRABLE, CATEGORY)            \
-  ENUM,
-#define ANALYSISSTART
-#include "clang/Basic/DiagnosticAnalysisKinds.inc"
-#undef DIAG
-  NUM_BUILTIN_ANALYSIS_DIAGNOSTICS
-};
-#define DIAG_ENUM(ENUM_NAME)                                                   \
-  namespace ENUM_NAME {                                                        \
-  enum {
-#define DIAG_ENUM_ITEM(IDX, NAME) NAME = IDX,
-#define DIAG_ENUM_END()                                                        \
-  }                                                                            \
-  ;                                                                            \
-  }
-#include "clang/Basic/DiagnosticAnalysisEnums.inc"
-#undef DIAG_ENUM_END
-#undef DIAG_ENUM_ITEM
-#undef DIAG_ENUM
-} // end namespace diag
-
-namespace diag_compat {
-#define DIAG_COMPAT_IDS_BEGIN() enum {
-#define DIAG_COMPAT_IDS_END()                                                  \
-  }                                                                            \
-  ;
-#define DIAG_COMPAT_ID(IDX, NAME, ...) NAME = IDX,
-#include "clang/Basic/DiagnosticAnalysisCompatIDs.inc"
-#undef DIAG_COMPAT_ID
-#undef DIAG_COMPAT_IDS_BEGIN
-#undef DIAG_COMPAT_IDS_END
-} // end namespace diag_compat
-} // end namespace clang
+#include "clang/Basic/DiagnosticAnalysisInterface.inc"
 
 #endif // LLVM_CLANG_BASIC_DIAGNOSTICANALYSIS_H
diff --git a/clang/include/clang/Basic/DiagnosticComment.h b/clang/include/clang/Basic/DiagnosticComment.h
index 08e66e8051834..f2a325c4e167a 100644
--- a/clang/include/clang/Basic/DiagnosticComment.h
+++ b/clang/include/clang/Basic/DiagnosticComment.h
@@ -10,44 +10,6 @@
 #define LLVM_CLANG_BASIC_DIAGNOSTICCOMMENT_H
 
 #include "clang/Basic/Diagnostic.h"
-
-namespace clang {
-namespace diag {
-enum {
-#define DIAG(ENUM, FLAGS, DEFAULT_MAPPING, DESC, GROUP, SFINAE, NOWERROR,      \
-             SHOWINSYSHEADER, SHOWINSYSMACRO, DEFERRABLE, CATEGORY)            \
-  ENUM,
-#define COMMENTSTART
-#include "clang/Basic/DiagnosticCommentKinds.inc"
-#undef DIAG
-  NUM_BUILTIN_COMMENT_DIAGNOSTICS
-};
-
-#define DIAG_ENUM(ENUM_NAME)                                                   \
-  namespace ENUM_NAME {                                                        \
-  enum {
-#define DIAG_ENUM_ITEM(IDX, NAME) NAME = IDX,
-#define DIAG_ENUM_END()                                                        \
-  }                                                                            \
-  ;                                                                            \
-  }
-#include "clang/Basic/DiagnosticCommentEnums.inc"
-#undef DIAG_ENUM_END
-#undef DIAG_ENUM_ITEM
-#undef DIAG_ENUM
-} // end namespace diag
-
-namespace diag_compat {
-#define DIAG_COMPAT_IDS_BEGIN() enum {
-#define DIAG_COMPAT_IDS_END()                                                  \
-  }                                                                            \
-  ;
-#define DIAG_COMPAT_ID(IDX, NAME, ...) NAME = IDX,
-#include "clang/Basic/DiagnosticCommentCompatIDs.inc"
-#undef DIAG_COMPAT_ID
-#undef DIAG_COMPAT_IDS_BEGIN
-#undef DIAG_COMPAT_IDS_END
-} // end namespace diag_compat
-} // end namespace clang
+#include "clang/Basic/DiagnosticCommentInterface.inc"
 
 #endif // LLVM_CLANG_BASIC_DIAGNOSTICCOMMENT_H
diff --git a/clang/include/clang/Basic/DiagnosticCrossTU.h b/clang/include/clang/Basic/DiagnosticCrossTU.h
index 761716d781446..4c0aa8cd26698 100644
--- a/clang/include/clang/Basic/DiagnosticCrossTU.h
+++ b/clang/include/clang/Basic/DiagnosticCrossTU.h
@@ -10,44 +10,6 @@
 #define LLVM_CLANG_BASIC_DIAGNOSTICCROSSTU_H
 
 #include "clang/Basic/Diagnostic.h"
-
-namespace clang {
-namespace diag {
-enum {
-#define DIAG(ENUM, FLAGS, DEFAULT_MAPPING, DESC, GROUP, SFINAE, NOWERROR,      \
-             SHOWINSYSHEADER, SHOWINSYSMACRO, DEFERRABLE, CATEGORY)            \
-  ENUM,
-#define CROSSTUSTART
-#include "clang/Basic/DiagnosticCrossTUKinds.inc"
-#undef DIAG
-  NUM_BUILTIN_CROSSTU_DIAGNOSTICS
-};
-
-#define DIAG_ENUM(ENUM_NAME)                                                   \
-  namespace ENUM_NAME {                                                        \
-  enum {
-#define DIAG_ENUM_ITEM(IDX, NAME) NAME = IDX,
-#define DIAG_ENUM_END()                                                        \
-  }                                                                            \
-  ;                                                                            \
-  }
-#include "clang/Basic/DiagnosticCrossTUEnums.inc"
-#undef DIAG_ENUM_END
-#undef DIAG_ENUM_ITEM
-#undef DIAG_ENUM
-} // end namespace diag
-
-namespace diag_compat {
-#define DIAG_COMPAT_IDS_BEGIN() enum {
-#define DIAG_COMPAT_IDS_END()                                                  \
-  }                                                                            \
-  ;
-#define DIAG_COMPAT_ID(IDX, NAME, ...) NAME = IDX,
-#include "clang/Basic/DiagnosticCrossTUCompatIDs.inc"
-#undef DIAG_COMPAT_ID
-#undef DIAG_COMPAT_IDS_BEGIN
-#undef DIAG_COMPAT_IDS_END
-} // end namespace diag_compat
-} // end namespace clang
+#include "clang/Basic/DiagnosticCrossTUInterface.inc"
 
 #endif // LLVM_CLANG_BASIC_DIAGNOSTICCROSSTU_H
diff --git a/clang/include/clang/Basic/DiagnosticDriver.h b/clang/include/clang/Basic/DiagnosticDriver.h
index 864a23a49e4cd..e00dcedd68ab1 100644
--- a/clang/include/clang/Basic/DiagnosticDriver.h
+++ b/clang/include/clang/Basic/DiagnosticDriver.h
@@ -10,44 +10,6 @@
 #define LLVM_CLANG_BASIC_DIAGNOSTICDRIVER_H
 
 #include "clang/Basic/Diagnostic.h"
-
-namespace clang {
-namespace diag {
-enum {
-#define DIAG(ENUM, FLAGS, DEFAULT_MAPPING, DESC, GROUP, SFINAE, NOWERROR,      \
-             SHOWINSYSHEADER, SHOWINSYSMACRO, DEFERRABLE, CATEGORY)            \
-  ENUM,
-#define DRIVERSTART
-#include "clang/Basic/DiagnosticDriverKinds.inc"
-#undef DIAG
-  NUM_BUILTIN_DRIVER_DIAGNOSTICS
-};
-
-#define DIAG_ENUM(ENUM_NAME)                                                   \
-  namespace ENUM_NAME {                                                        \
-  enum {
-#define DIAG_ENUM_ITEM(IDX, NAME) NAME = IDX,
-#define DIAG_ENUM_END()                                                        \
-  }                                                                            \
-  ;                                                                            \
-  }
-#include "clang/Basic/DiagnosticDriverEnums.inc"
-#undef DIAG_ENUM_END
-#undef DIAG_ENUM_ITEM
-#undef DIAG_ENUM
-} // end namespace diag
-
-namespace diag_compat {
-#define DIAG_COMPAT_IDS_BEGIN() enum {
-#define DIAG_COMPAT_IDS_END()                                                  \
-  }                                                                            \
-  ;
-#define DIAG_COMPAT_ID(IDX, NAME, ...) NAME = IDX,
-#include "clang/Basic/DiagnosticDriverCompatIDs.inc"
-#undef DIAG_COMPAT_ID
-#undef DIAG_COMPAT_IDS_BEGIN
-#undef DIAG_COMPAT_IDS_END
-} // end namespace diag_compat
-} // end namespace clang
+#include "clang/Basic/DiagnosticDriverInterface.inc"
 
 #endif // LLVM_CLANG_BASIC_DIAGNOSTICDRIVER_H
diff --git a/clang/include/clang/Basic/DiagnosticFrontend.h b/clang/include/clang/Basic/DiagnosticFrontend.h
index 3506f05daae54..923ddd3b45ba0 100644
--- a/clang/include/clang/Basic/DiagnosticFrontend.h
+++ b/clang/include/clang/Basic/DiagnosticFrontend.h
@@ -10,44 +10,6 @@
 #define LLVM_CLANG_BASIC_DIAGNOSTICFRONTEND_H
 
 #include "clang/Basic/Diagnostic.h"
-
-namespace clang {
-namespace diag {
-enum {
-#define DIAG(ENUM, FLAGS, DEFAULT_MAPPING, DESC, GROUP, SFINAE, NOWERROR,      \
-             SHOWINSYSHEADER, SHOWINSYSMACRO, DEFERRABLE, CATEGORY)            \
-  ENUM,
-#define FRONTENDSTART
-#include "clang/Basic/DiagnosticFrontendKinds.inc"
-#undef DIAG
-  NUM_BUILTIN_FRONTEND_DIAGNOSTICS
-};
-
-#define DIAG_ENUM(ENUM_NAME)                                                   \
-  namespace ENUM_NAME {                                                        \
-  enum {
-#define DIAG_ENUM_ITEM(IDX, NAME) NAME = IDX,
-#define DIAG_ENUM_END()                                                        \
-  }                                                                            \
-  ;                                                                            \
-  }
-#include "clang/Basic/DiagnosticFrontendEnums.inc"
-#undef DIAG_ENUM_END
-#undef DIAG_ENUM_ITEM
-#undef DIAG_ENUM
-} // end namespace diag
-
-namespace diag_compat {
-#define DIAG_COMPAT_IDS_BEGIN() enum {
-#define DIAG_COMPAT_IDS_END()                                                  \
-  }                                                                            \
-  ;
-#define DIAG_COMPAT_ID(IDX, NAME, ...) NAME = IDX,
-#include "clang/Basic/DiagnosticFrontendCompatIDs.inc"
-#undef DIAG_COMPAT_ID
-#undef DIAG_COMPAT_IDS_BEGIN
-#undef DIAG_COMPAT_IDS_END
-} // end namespace diag_compat
-} // end namespace clang
+#include "clang/Basic/DiagnosticFrontendInterface.inc"
 
 #endif // LLVM_CLANG_BASIC_DIAGNOSTICFRONTEND_H
diff --git a/clang/include/clang/Basic/DiagnosticIDs.h b/clang/include/clang/Basic/DiagnosticIDs.h
index f936d4fb7a403..80d52a0d01112 100644
--- a/clang/include/clang/Basic/DiagnosticIDs.h
+++ b/clang/include/clang/Basic/DiagnosticIDs.h
@@ -71,17 +71,6 @@ namespace clang {
     /// All of the diagnostics that can be emitted by the frontend.
     typedef unsigned kind;
 
-    // Get typedefs for common diagnostics.
-    enum {
-#define DIAG(ENUM, FLAGS, DEFAULT_MAPPING, DESC, GROUP, SFINAE, CATEGORY,      \
-             NOWERROR, SHOWINSYSHEADER, SHOWINSYSMACRO, DEFFERABLE)            \
-  ENUM,
-#define COMMONSTART
-#include "clang/Basic/DiagnosticCommonKinds.inc"
-      NUM_BUILTIN_COMMON_DIAGNOSTICS
-#undef DIAG
-    };
-
     /// Enum values that allow the client to map NOTEs, WARNINGs, and EXTENSIONs
     /// to either Ignore (nothing), Remark (emit a remark), Warning
     /// (emit a warning) or Error (emit as an error).  It allows clients to
@@ -103,20 +92,13 @@ namespace clang {
       Remark          ///< A diagnostic that indicates normal progress through
                       ///< compilation.
     };
-  }
+  } // end namespace diag
+} // end namespace clang
 
-  namespace diag_compat {
-#define DIAG_COMPAT_IDS_BEGIN() enum {
-#define DIAG_COMPAT_IDS_END()                                                \
-    }                                                                          \
-    ;
-#define DIAG_COMPAT_ID(IDX, NAME, ...) NAME = IDX,
-#include "clang/Basic/DiagnosticCommonCompatIDs.inc"
-#undef DIAG_COMPAT_ID
-#undef DIAG_COMPAT_IDS_BEGIN
-#undef DIAG_COMPAT_IDS_END
-  } // end namespace diag_compat
+// This has to be included *after* the DIAG_START_ enums above are defined.
+#include "clang/Basic/DiagnosticCommonInterface.inc"
 
+namespace clang {
 class DiagnosticMapping {
   LLVM_PREFERRED_TYPE(diag::Severity)
   unsigned Severity : 3;
diff --git a/clang/include/clang/Basic/DiagnosticInstallAPI.h b/clang/include/clang/Basic/DiagnosticInstallAPI.h
index 4619bfeea05a2..9d814522270e5 100644
--- a/clang/include/clang/Basic/DiagnosticInstallAPI.h
+++ b/clang/include/clang/Basic/DiagnosticInstallAPI.h
@@ -10,42 +10,6 @@
 #define LLVM_CLANG_BASIC_DIAGNOSTICINSTALLAPI_H
 
 #include "clang/Basic/Diagnostic.h"
-namespace clang {
-namespace diag {
-enum {
-#define DIAG(ENUM, FLAGS, DEFAULT_MAPPING, DESC, GROUP, SFINAE, NOWERROR,      \
-             SHOWINSYSHEADER, SHOWINSYSMACRO, DEFERRABLE, CATEGORY)            \
-  ENUM,
-#define INSTALLAPISTART
-#include "clang/Basic/DiagnosticInstallAPIKinds.inc"
-#undef DIAG
-  NUM_BUILTIN_INSTALLAPI_DIAGNOSTICS
-};
+#include "clang/Basic/DiagnosticInstallAPIInterface.inc"
 
-#define DIAG_ENUM(ENUM_NAME)                                                   \
-  namespace ENUM_NAME {                                                        \
-  enum {
-#define DIAG_ENUM_ITEM(IDX, NAME) NAME = IDX,
-#define DIAG_ENUM_END()                                                        \
-  }                                                                            \
-  ;                                                                            \
-  }
-#include "clang/Basic/DiagnosticInstallAPIEnums.inc"
-#undef DIAG_ENUM_END
-#undef DIAG_ENUM_ITEM
-#undef DIAG_ENUM
-} // namespace diag
-
-namespace diag_compat {
-#define DIAG_COMPAT_IDS_BEGIN() enum {
-#define DIAG_COMPAT_IDS_END()                                                  \
-  }                                                                            \
-  ;
-#define DIAG_COMPAT_ID(IDX, NAME, ...) NAME = IDX,
-#include "clang/Basic/DiagnosticInstallAPICompatIDs.inc"
-#undef DIAG_COMPAT_ID
-#undef DIAG_COMPAT_IDS_BEGIN
-#undef DIAG_COMPAT_IDS_END
-} // end namespace diag_compat
-} // namespace clang
 #endif // LLVM_CLANG_BASIC_DIAGNOSTICINSTALLAPI_H
diff --git a/clang/include/clang/Basic/DiagnosticLex.h b/clang/include/clang/Basic/DiagnosticLex.h
index 6fa90f785bbf8..f20f4fcd2d3d7 100644
--- a/clang/include/clang/Basic/DiagnosticLex.h
+++ b/clang/include/clang/Basic/DiagnosticLex.h
@@ -10,43 +10,6 @@
 #define LLVM_CLANG_BASIC_DIAGNOSTICLEX_H
 
 #include "clang/Basic/Diagnostic.h"
-
-namespace clang {
-namespace diag {
-enum {
-#define DIAG(ENUM, FLAGS, DEFAULT_MAPPING, DESC, GROUP, SFINAE, NOWERROR,      \
-             SHOWINSYSHEADER, SHOWINSYSMACRO, DEFERRABLE, CATEGORY)            \
-  ENUM,
-#define LEXSTART
-#include "clang/Basic/DiagnosticLexKinds.inc"
-#undef DIAG
-  NUM_BUILTIN_LEX_DIAGNOSTICS
-};
-#define DIAG_ENUM(ENUM_NAME)                                                   \
-  namespace ENUM_NAME {                                                        \
-  enum {
-#define DIAG_ENUM_ITEM(IDX, NAME) NAME = IDX,
-#define DIAG_ENUM_END()                                                        \
-  }                                                                            \
-  ;                                                                            \
-  }
-#include "clang/Basic/DiagnosticLexEnums.inc"
-#undef DIAG_ENUM_END
-#undef DIAG_ENUM_ITEM
-#undef DIAG_ENUM
-} // end namespace diag
-
-namespace diag_compat {
-#define DIAG_COMPAT_IDS_BEGIN() enum {
-#define DIAG_COMPAT_IDS_END()                                                  \
-  }                                                                            \
-  ;
-#define DIAG_COMPAT_ID(IDX, NAME, ...) NAME = IDX,
-#include "clang/Basic/DiagnosticLexCompatIDs.inc"
-#undef DIAG_COMPAT_ID
-#undef DIAG_COMPAT_IDS_BEGIN
-#undef DIAG_COMPAT_IDS_END
-} // end namespace diag_compat
-} // end namespace clang
+#include "clang/Basic/DiagnosticLexInterface.inc"
 
 #endif // LLVM_CLANG_BASIC_DIAGNOSTICLEX_H
diff --git a/clang/include/clang/Basic/DiagnosticParse.h b/clang/include/clang/Basic/DiagnosticParse.h
index e2a4368a59c4b..1e025bdf02415 100644
--- a/clang/include/clang/Basic/DiagnosticParse.h
+++ b/clang/include/clang/Basic/DiagnosticParse.h
@@ -10,44 +10,6 @@
 #define LLVM_CLANG_BASIC_DIAGNOSTICPARSE_H
 
 #include "clang/Basic/Diagnostic.h"
-
-namespace clang {
-namespace diag {
-enum {
-#define DIAG(ENUM, FLAGS, DEFAULT_MAPPING, DESC, GROUP, SFINAE, NOWERROR,      \
-             SHOWINSYSHEADER, SHOWINSYSMACRO, DEFERRABLE, CATEGORY)            \
-  ENUM,
-#define PARSESTART
-#include "clang/Basic/DiagnosticParseKinds.inc"
-#undef DIAG
-  NUM_BUILTIN_PARSE_DIAGNOSTICS
-};
-
-#define DIAG_ENUM(ENUM_NAME)                                                   \
-  namespace ENUM_NAME {                                                        \
-  enum {
-#define DIAG_ENUM_ITEM(IDX, NAME) NAME = IDX,
-#define DIAG_ENUM_END()                                                        \
-  }                                                                            \
-  ;                                                                            \
-  }
-#include "clang/Basic/DiagnosticParseEnums.inc"
-#undef DIAG_ENUM_END
-#undef DIAG_ENUM_ITEM
-#undef DIAG_ENUM
-} // end namespace diag
-
-namespace diag_compat {
-#define DIAG_COMPAT_IDS_BEGIN() enum {
-#define DIAG_COMPAT_IDS_END()                                                  \
-  }                                                                            \
-  ;
-#define DIAG_COMPAT_ID(IDX, NAME, ...) NAME = IDX,
-#include "clang/Basic/DiagnosticParseCompatIDs.inc"
-#undef DIAG_COMPAT_ID
-#undef DIAG_COMPAT_IDS_BEGIN
-#undef DIAG_COMPAT_IDS_END
-} // end namespace diag_compat
-} // end namespace clang
+#include "clang/Basic/DiagnosticParseInterface.inc"
 
 #endif // LLVM_CLANG_BASIC_DIAGNOSTICPARSE_H
diff --git a/clang/include/clang/Basic/DiagnosticRefactoring.h b/clang/include/clang/Basic/DiagnosticRefactoring.h
index b3f3a10925f09..380060e51d0f6 100644
--- a/clang/include/clang/Basic/DiagnosticRefactoring.h
+++ b/clang/include/clang/Basic/DiagnosticRefactoring.h
@@ -10,44 +10,6 @@
 #define LLVM_CLANG_BASIC_DIAGNOSTICREFACTORING_H
 
 #include "clang/Basic/Diagnostic.h"
-
-namespace clang {
-namespace diag {
-enum {
-#define DIAG(ENUM, FLAGS, DEFAULT_MAPPING, DESC, GROUP, SFINAE, NOWERROR,      \
-             SHOWINSYSHEADER, SHOWINSYSMACRO, DEFERRABLE, CATEGORY)            \
-  ENUM,
-#define REFACTORINGSTART
-#include "clang/Basic/DiagnosticRefactoringKinds.inc"
-#undef DIAG
-  NUM_BUILTIN_REFACTORING_DIAGNOSTICS
-};
-
-#define DIAG_ENUM(ENUM_NAME)                                                   \
-  namespace ENUM_NAME {                                                 ...
[truncated]

Copy link

github-actions bot commented Apr 8, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions cpp,h -- clang/include/clang/Basic/DiagnosticAST.h clang/include/clang/Basic/DiagnosticAnalysis.h clang/include/clang/Basic/DiagnosticComment.h clang/include/clang/Basic/DiagnosticCrossTU.h clang/include/clang/Basic/DiagnosticDriver.h clang/include/clang/Basic/DiagnosticFrontend.h clang/include/clang/Basic/DiagnosticIDs.h clang/include/clang/Basic/DiagnosticInstallAPI.h clang/include/clang/Basic/DiagnosticLex.h clang/include/clang/Basic/DiagnosticParse.h clang/include/clang/Basic/DiagnosticRefactoring.h clang/include/clang/Basic/DiagnosticSema.h clang/include/clang/Basic/DiagnosticSerialization.h clang/utils/TableGen/ClangDiagnosticsEmitter.cpp clang/utils/TableGen/TableGen.cpp clang/utils/TableGen/TableGenBackends.h
View the diff from clang-format here.
diff --git a/clang/include/clang/Basic/DiagnosticIDs.h b/clang/include/clang/Basic/DiagnosticIDs.h
index 80d52a0d0..83891ff11 100644
--- a/clang/include/clang/Basic/DiagnosticIDs.h
+++ b/clang/include/clang/Basic/DiagnosticIDs.h
@@ -92,83 +92,86 @@ namespace clang {
       Remark          ///< A diagnostic that indicates normal progress through
                       ///< compilation.
     };
-  } // end namespace diag
-} // end namespace clang
+    } // end namespace diag
+    } // end namespace clang
 
-// This has to be included *after* the DIAG_START_ enums above are defined.
+    // This has to be included *after* the DIAG_START_ enums above are defined.
 #include "clang/Basic/DiagnosticCommonInterface.inc"
 
-namespace clang {
-class DiagnosticMapping {
-  LLVM_PREFERRED_TYPE(diag::Severity)
-  unsigned Severity : 3;
-  LLVM_PREFERRED_TYPE(bool)
-  unsigned IsUser : 1;
-  LLVM_PREFERRED_TYPE(bool)
-  unsigned IsPragma : 1;
-  LLVM_PREFERRED_TYPE(bool)
-  unsigned HasNoWarningAsError : 1;
-  LLVM_PREFERRED_TYPE(bool)
-  unsigned HasNoErrorAsFatal : 1;
-  LLVM_PREFERRED_TYPE(bool)
-  unsigned WasUpgradedFromWarning : 1;
-
-public:
-  static DiagnosticMapping Make(diag::Severity Severity, bool IsUser,
-                                bool IsPragma) {
-    DiagnosticMapping Result;
-    Result.Severity = (unsigned)Severity;
-    Result.IsUser = IsUser;
-    Result.IsPragma = IsPragma;
-    Result.HasNoWarningAsError = 0;
-    Result.HasNoErrorAsFatal = 0;
-    Result.WasUpgradedFromWarning = 0;
-    return Result;
-  }
+    namespace clang {
+    class DiagnosticMapping {
+      LLVM_PREFERRED_TYPE(diag::Severity)
+      unsigned Severity : 3;
+      LLVM_PREFERRED_TYPE(bool)
+      unsigned IsUser : 1;
+      LLVM_PREFERRED_TYPE(bool)
+      unsigned IsPragma : 1;
+      LLVM_PREFERRED_TYPE(bool)
+      unsigned HasNoWarningAsError : 1;
+      LLVM_PREFERRED_TYPE(bool)
+      unsigned HasNoErrorAsFatal : 1;
+      LLVM_PREFERRED_TYPE(bool)
+      unsigned WasUpgradedFromWarning : 1;
+
+    public:
+      static DiagnosticMapping Make(diag::Severity Severity, bool IsUser,
+                                    bool IsPragma) {
+        DiagnosticMapping Result;
+        Result.Severity = (unsigned)Severity;
+        Result.IsUser = IsUser;
+        Result.IsPragma = IsPragma;
+        Result.HasNoWarningAsError = 0;
+        Result.HasNoErrorAsFatal = 0;
+        Result.WasUpgradedFromWarning = 0;
+        return Result;
+      }
 
-  diag::Severity getSeverity() const { return (diag::Severity)Severity; }
-  void setSeverity(diag::Severity Value) { Severity = (unsigned)Value; }
+      diag::Severity getSeverity() const { return (diag::Severity)Severity; }
+      void setSeverity(diag::Severity Value) { Severity = (unsigned)Value; }
 
-  bool isUser() const { return IsUser; }
-  bool isPragma() const { return IsPragma; }
+      bool isUser() const { return IsUser; }
+      bool isPragma() const { return IsPragma; }
 
-  bool isErrorOrFatal() const {
-    return getSeverity() == diag::Severity::Error ||
-           getSeverity() == diag::Severity::Fatal;
-  }
+      bool isErrorOrFatal() const {
+        return getSeverity() == diag::Severity::Error ||
+               getSeverity() == diag::Severity::Fatal;
+      }
 
-  bool hasNoWarningAsError() const { return HasNoWarningAsError; }
-  void setNoWarningAsError(bool Value) { HasNoWarningAsError = Value; }
+      bool hasNoWarningAsError() const { return HasNoWarningAsError; }
+      void setNoWarningAsError(bool Value) { HasNoWarningAsError = Value; }
 
-  bool hasNoErrorAsFatal() const { return HasNoErrorAsFatal; }
-  void setNoErrorAsFatal(bool Value) { HasNoErrorAsFatal = Value; }
+      bool hasNoErrorAsFatal() const { return HasNoErrorAsFatal; }
+      void setNoErrorAsFatal(bool Value) { HasNoErrorAsFatal = Value; }
 
-  /// Whether this mapping attempted to map the diagnostic to a warning, but
-  /// was overruled because the diagnostic was already mapped to an error or
-  /// fatal error.
-  bool wasUpgradedFromWarning() const { return WasUpgradedFromWarning; }
-  void setUpgradedFromWarning(bool Value) { WasUpgradedFromWarning = Value; }
+      /// Whether this mapping attempted to map the diagnostic to a warning, but
+      /// was overruled because the diagnostic was already mapped to an error or
+      /// fatal error.
+      bool wasUpgradedFromWarning() const { return WasUpgradedFromWarning; }
+      void setUpgradedFromWarning(bool Value) {
+        WasUpgradedFromWarning = Value;
+      }
 
-  /// Serialize this mapping as a raw integer.
-  unsigned serialize() const {
-    return (IsUser << 7) | (IsPragma << 6) | (HasNoWarningAsError << 5) |
-           (HasNoErrorAsFatal << 4) | (WasUpgradedFromWarning << 3) | Severity;
-  }
-  /// Deserialize a mapping.
-  static DiagnosticMapping deserialize(unsigned Bits) {
-    DiagnosticMapping Result;
-    Result.IsUser = (Bits >> 7) & 1;
-    Result.IsPragma = (Bits >> 6) & 1;
-    Result.HasNoWarningAsError = (Bits >> 5) & 1;
-    Result.HasNoErrorAsFatal = (Bits >> 4) & 1;
-    Result.WasUpgradedFromWarning = (Bits >> 3) & 1;
-    Result.Severity = Bits & 0x7;
-    return Result;
-  }
+      /// Serialize this mapping as a raw integer.
+      unsigned serialize() const {
+        return (IsUser << 7) | (IsPragma << 6) | (HasNoWarningAsError << 5) |
+               (HasNoErrorAsFatal << 4) | (WasUpgradedFromWarning << 3) |
+               Severity;
+      }
+      /// Deserialize a mapping.
+      static DiagnosticMapping deserialize(unsigned Bits) {
+        DiagnosticMapping Result;
+        Result.IsUser = (Bits >> 7) & 1;
+        Result.IsPragma = (Bits >> 6) & 1;
+        Result.HasNoWarningAsError = (Bits >> 5) & 1;
+        Result.HasNoErrorAsFatal = (Bits >> 4) & 1;
+        Result.WasUpgradedFromWarning = (Bits >> 3) & 1;
+        Result.Severity = Bits & 0x7;
+        return Result;
+      }
 
-  bool operator==(DiagnosticMapping Other) const {
-    return serialize() == Other.serialize();
-  }
+      bool operator==(DiagnosticMapping Other) const {
+        return serialize() == Other.serialize();
+      }
 };
 
 /// Used for handling and querying diagnostic IDs.
@@ -502,6 +505,6 @@ private:
   friend class DiagnosticsEngine;
 };
 
-}  // end namespace clang
+} // end namespace clang
 
 #endif

@Sirraide
Copy link
Member Author

Sirraide commented Apr 8, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
View the diff from clang-format here.

clang-format seems to be having a stroke here. It’s suggesting indenting namespace clang { and everything in it even though it’s a top-level namespace defintion. I think this might be because the rest of the file is already quite weirdly formatted: the namespace diag { ... } block above the #include line is indented even though we don’t usually do that (anymore), which might be confusing it.

I think we should just ignore this because it’d probably take reformatting the entire file (or at least everything before the #include that this patch adds) to fix this.

Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this!

const char *Upper = ComponentUpper.c_str();

OS << llvm::format(R"c++(
namespace clang {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are all so common (differing only by the one name change) that I wonder if tablegen is the right place for this, instead of just a predefined file. BUT I can't htink of a way to do that which doesn't require preprocessor string-replacement inside of a #include or define identifier, which, IIRC is UB?

SO I guess this is ok and the only way we can reasonably do this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BUT I can't htink of a way to do that which doesn't require preprocessor string-replacement inside of a #include or define identifier, which, IIRC is UB?

Yeah, I think the preprocessor directives are the main problem, e.g. the #define ASTSTART; there isn’t really a way to do this just in the preprocessor unfortunately...

} // end namespace diag_compat
} // end namespace clang
)c++",
Upper, Comp, Upper, Comp, Comp);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an unfortunate part of this interface... Does llvm::format not support the std::format positional arguments?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an unfortunate part of this interface... Does llvm::format not support the std::format positional arguments?

It uses snprintf... which doesn’t support them on Windows. I tried...

@Sirraide Sirraide merged commit 6c74fe9 into llvm:main Apr 8, 2025
10 of 11 checks passed
@Sirraide Sirraide deleted the diag-headers-tablegen branch April 8, 2025 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants