Skip to content

Commit bf17016

Browse files
Add 'enum_select' diagnostic selection to clang. (#122505)
This causes us to generate an enum to go along with the select diagnostic, which allows for clearer diagnostic error emit lines. The syntax for this is: %enum_select<EnumerationName>{%OptionalEnumeratorName{Text}|{Text2}}0 Where the curley brackets around the select-text are only required if an Enumerator name is provided. The TableGen here emits this as a normal 'select' to the frontend, which permits us to reuse all of the existing 'select' infrastructure. Documentation is the same as well. --------- Co-authored-by: Aaron Ballman <[email protected]>
1 parent d15d410 commit bf17016

21 files changed

+464
-17
lines changed

clang/docs/InternalsManual.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,21 @@ Description:
276276
diagnostic instead of having to do things textually. The selected string
277277
does undergo formatting.
278278

279+
**"enum_select format**
280+
281+
Example:
282+
``unknown frobbling of a %enum_select<FrobbleKind>{%VarDecl{variable declaration}|%FuncDecl{function declaration}}0 when blarging``
283+
Class:
284+
Integers
285+
Description:
286+
This format specifier is used exactly like a ``select`` specifier, except it
287+
additionally generates a namespace, enumeration, and enumerator list based on
288+
the format string given. In the above case, a namespace is generated named
289+
``FrobbleKind`` that has an unscoped enumeration with the enumerators
290+
``VarDecl`` and ``FuncDecl`` which correspond to the values 0 and 1. This
291+
permits a clearer use of the ``Diag`` in source code, as the above could be
292+
called as: ``Diag(Loc, diag::frobble) << diag::FrobbleKind::VarDecl``.
293+
279294
**"plural" format**
280295

281296
Example:

clang/include/clang/Basic/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ macro(clang_diag_gen component)
33
-gen-clang-diags-defs -clang-component=${component}
44
SOURCE Diagnostic.td
55
TARGET ClangDiagnostic${component})
6+
7+
clang_tablegen(Diagnostic${component}Enums.inc
8+
-gen-clang-diags-enums -clang-component=${component}
9+
SOURCE Diagnostic.td
10+
TARGET ClangDiagnostic${component}Enums)
611
endmacro(clang_diag_gen)
712

813
clang_diag_gen(Analysis)

clang/include/clang/Basic/DiagnosticAST.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ enum {
2222
#undef DIAG
2323
NUM_BUILTIN_AST_DIAGNOSTICS
2424
};
25+
26+
#define DIAG_ENUM(ENUM_NAME) \
27+
namespace ENUM_NAME { \
28+
enum {
29+
#define DIAG_ENUM_ITEM(IDX, NAME) NAME = IDX,
30+
#define DIAG_ENUM_END() \
31+
} \
32+
; \
33+
}
34+
#include "clang/Basic/DiagnosticASTEnums.inc"
35+
#undef DIAG_ENUM_END
36+
#undef DIAG_ENUM_ITEM
37+
#undef DIAG_ENUM
2538
} // end namespace diag
2639
} // end namespace clang
2740

clang/include/clang/Basic/DiagnosticAnalysis.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ enum {
2222
#undef DIAG
2323
NUM_BUILTIN_ANALYSIS_DIAGNOSTICS
2424
};
25+
#define DIAG_ENUM(ENUM_NAME) \
26+
namespace ENUM_NAME { \
27+
enum {
28+
#define DIAG_ENUM_ITEM(IDX, NAME) NAME = IDX,
29+
#define DIAG_ENUM_END() \
30+
} \
31+
; \
32+
}
33+
#include "clang/Basic/DiagnosticAnalysisEnums.inc"
34+
#undef DIAG_ENUM_END
35+
#undef DIAG_ENUM_ITEM
36+
#undef DIAG_ENUM
2537
} // end namespace diag
2638
} // end namespace clang
2739

clang/include/clang/Basic/DiagnosticComment.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ enum {
2222
#undef DIAG
2323
NUM_BUILTIN_COMMENT_DIAGNOSTICS
2424
};
25+
26+
#define DIAG_ENUM(ENUM_NAME) \
27+
namespace ENUM_NAME { \
28+
enum {
29+
#define DIAG_ENUM_ITEM(IDX, NAME) NAME = IDX,
30+
#define DIAG_ENUM_END() \
31+
} \
32+
; \
33+
}
34+
#include "clang/Basic/DiagnosticCommentEnums.inc"
35+
#undef DIAG_ENUM_END
36+
#undef DIAG_ENUM_ITEM
37+
#undef DIAG_ENUM
2538
} // end namespace diag
2639
} // end namespace clang
2740

clang/include/clang/Basic/DiagnosticCrossTU.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ enum {
2222
#undef DIAG
2323
NUM_BUILTIN_CROSSTU_DIAGNOSTICS
2424
};
25+
26+
#define DIAG_ENUM(ENUM_NAME) \
27+
namespace ENUM_NAME { \
28+
enum {
29+
#define DIAG_ENUM_ITEM(IDX, NAME) NAME = IDX,
30+
#define DIAG_ENUM_END() \
31+
} \
32+
; \
33+
}
34+
#include "clang/Basic/DiagnosticCrossTUEnums.inc"
35+
#undef DIAG_ENUM_END
36+
#undef DIAG_ENUM_ITEM
37+
#undef DIAG_ENUM
2538
} // end namespace diag
2639
} // end namespace clang
2740

clang/include/clang/Basic/DiagnosticDriver.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ enum {
2222
#undef DIAG
2323
NUM_BUILTIN_DRIVER_DIAGNOSTICS
2424
};
25+
26+
#define DIAG_ENUM(ENUM_NAME) \
27+
namespace ENUM_NAME { \
28+
enum {
29+
#define DIAG_ENUM_ITEM(IDX, NAME) NAME = IDX,
30+
#define DIAG_ENUM_END() \
31+
} \
32+
; \
33+
}
34+
#include "clang/Basic/DiagnosticDriverEnums.inc"
35+
#undef DIAG_ENUM_END
36+
#undef DIAG_ENUM_ITEM
37+
#undef DIAG_ENUM
2538
} // end namespace diag
2639
} // end namespace clang
2740

clang/include/clang/Basic/DiagnosticFrontend.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ enum {
2222
#undef DIAG
2323
NUM_BUILTIN_FRONTEND_DIAGNOSTICS
2424
};
25+
26+
#define DIAG_ENUM(ENUM_NAME) \
27+
namespace ENUM_NAME { \
28+
enum {
29+
#define DIAG_ENUM_ITEM(IDX, NAME) NAME = IDX,
30+
#define DIAG_ENUM_END() \
31+
} \
32+
; \
33+
}
34+
#include "clang/Basic/DiagnosticFrontendEnums.inc"
35+
#undef DIAG_ENUM_END
36+
#undef DIAG_ENUM_ITEM
37+
#undef DIAG_ENUM
2538
} // end namespace diag
2639
} // end namespace clang
2740

clang/include/clang/Basic/DiagnosticInstallAPI.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ enum {
2121
#undef DIAG
2222
NUM_BUILTIN_INSTALLAPI_DIAGNOSTICS
2323
};
24+
25+
#define DIAG_ENUM(ENUM_NAME) \
26+
namespace ENUM_NAME { \
27+
enum {
28+
#define DIAG_ENUM_ITEM(IDX, NAME) NAME = IDX,
29+
#define DIAG_ENUM_END() \
30+
} \
31+
; \
32+
}
33+
#include "clang/Basic/DiagnosticInstallAPIEnums.inc"
34+
#undef DIAG_ENUM_END
35+
#undef DIAG_ENUM_ITEM
36+
#undef DIAG_ENUM
2437
} // namespace diag
2538
} // namespace clang
2639
#endif // LLVM_CLANG_BASIC_DIAGNOSTICINSTALLAPI_H

clang/include/clang/Basic/DiagnosticLex.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ enum {
2222
#undef DIAG
2323
NUM_BUILTIN_LEX_DIAGNOSTICS
2424
};
25+
#define DIAG_ENUM(ENUM_NAME) \
26+
namespace ENUM_NAME { \
27+
enum {
28+
#define DIAG_ENUM_ITEM(IDX, NAME) NAME = IDX,
29+
#define DIAG_ENUM_END() \
30+
} \
31+
; \
32+
}
33+
#include "clang/Basic/DiagnosticLexEnums.inc"
34+
#undef DIAG_ENUM_END
35+
#undef DIAG_ENUM_ITEM
36+
#undef DIAG_ENUM
2537
} // end namespace diag
2638
} // end namespace clang
2739

0 commit comments

Comments
 (0)