Skip to content

Add 'enum_select' diagnostic selection to clang. #122505

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 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions clang/docs/InternalsManual.rst
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,21 @@ Description:
diagnostic instead of having to do things textually. The selected string
does undergo formatting.

**"enum_select format**

Example:
``unknown frobbling of a %enum_select<FrobbleKind>{%VarDecl{variable declaration}|%FuncDecl{function declaration}}0 when blarging``
Class:
Integers
Description:
This format specifier is used exactly like a ``select`` specifier, except it
additionally generates a namespace, enumeration, and enumerator list based on
the format string given. In the above case, a namespace is generated named
``FrobbleKind`` that has an unscoped enumeration with the enumerators
``VarDecl`` and ``FuncDecl`` which correspond to the values 0 and 1. This
permits a clearer use of the ``Diag`` in source code, as the above could be
called as: ``Diag(Loc, diag::frobble) << diag::FrobbleKind::VarDecl``.

**"plural" format**

Example:
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/Basic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ macro(clang_diag_gen component)
-gen-clang-diags-defs -clang-component=${component}
SOURCE Diagnostic.td
TARGET ClangDiagnostic${component})

clang_tablegen(Diagnostic${component}Enums.inc
-gen-clang-diags-enums -clang-component=${component}
SOURCE Diagnostic.td
TARGET ClangDiagnostic${component}Enums)
endmacro(clang_diag_gen)

clang_diag_gen(Analysis)
Expand Down
13 changes: 13 additions & 0 deletions clang/include/clang/Basic/DiagnosticAST.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ enum {
#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
} // end namespace clang

Expand Down
12 changes: 12 additions & 0 deletions clang/include/clang/Basic/DiagnosticAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ enum {
#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
} // end namespace clang

Expand Down
13 changes: 13 additions & 0 deletions clang/include/clang/Basic/DiagnosticComment.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ enum {
#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
} // end namespace clang

Expand Down
13 changes: 13 additions & 0 deletions clang/include/clang/Basic/DiagnosticCrossTU.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ enum {
#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
} // end namespace clang

Expand Down
13 changes: 13 additions & 0 deletions clang/include/clang/Basic/DiagnosticDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ enum {
#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
} // end namespace clang

Expand Down
13 changes: 13 additions & 0 deletions clang/include/clang/Basic/DiagnosticFrontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ enum {
#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
} // end namespace clang

Expand Down
13 changes: 13 additions & 0 deletions clang/include/clang/Basic/DiagnosticInstallAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ enum {
#undef DIAG
NUM_BUILTIN_INSTALLAPI_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/DiagnosticInstallAPIEnums.inc"
#undef DIAG_ENUM_END
#undef DIAG_ENUM_ITEM
#undef DIAG_ENUM
} // namespace diag
} // namespace clang
#endif // LLVM_CLANG_BASIC_DIAGNOSTICINSTALLAPI_H
12 changes: 12 additions & 0 deletions clang/include/clang/Basic/DiagnosticLex.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ enum {
#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
} // end namespace clang

Expand Down
13 changes: 13 additions & 0 deletions clang/include/clang/Basic/DiagnosticParse.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ enum {
#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
} // end namespace clang

Expand Down
13 changes: 13 additions & 0 deletions clang/include/clang/Basic/DiagnosticRefactoring.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ enum {
#undef DIAG
NUM_BUILTIN_REFACTORING_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/DiagnosticRefactoringEnums.inc"
#undef DIAG_ENUM_END
#undef DIAG_ENUM_ITEM
#undef DIAG_ENUM
} // end namespace diag
} // end namespace clang

Expand Down
13 changes: 13 additions & 0 deletions clang/include/clang/Basic/DiagnosticSema.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ enum {
#undef DIAG
NUM_BUILTIN_SEMA_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/DiagnosticSemaEnums.inc"
#undef DIAG_ENUM_END
#undef DIAG_ENUM_ITEM
#undef DIAG_ENUM
} // end namespace diag
} // end namespace clang

Expand Down
8 changes: 5 additions & 3 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -617,9 +617,11 @@ def err_ambiguous_inherited_constructor : Error<
"constructor of %0 inherited from multiple base class subobjects">;
def note_ambiguous_inherited_constructor_using : Note<
"inherited from base class %0 here">;
def note_using_decl_class_member_workaround : Note<
"use %select{an alias declaration|a typedef declaration|a reference|"
"a const variable|a constexpr variable}0 instead">;
def note_using_decl_class_member_workaround
: Note<"use %enum_select<MemClassWorkaround>{%AliasDecl{an alias "
"declaration}|%TypedefDecl{a typedef declaration}|%ReferenceDecl{a "
"reference}|%ConstVar{a const variable}|%ConstexprVar{a constexpr "
"variable}}0 instead">;
def err_using_decl_can_not_refer_to_namespace : Error<
"using declaration cannot refer to a namespace">;
def note_namespace_using_decl : Note<
Expand Down
13 changes: 13 additions & 0 deletions clang/include/clang/Basic/DiagnosticSerialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ enum {
#undef DIAG
NUM_BUILTIN_SERIALIZATION_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/DiagnosticSerializationEnums.inc"
#undef DIAG_ENUM_END
#undef DIAG_ENUM_ITEM
#undef DIAG_ENUM
} // end namespace diag
} // end namespace clang

Expand Down
25 changes: 13 additions & 12 deletions clang/lib/Sema/SemaDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13217,18 +13217,18 @@ bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, bool HasTypename,
if (getLangOpts().CPlusPlus11) {
// Convert 'using X::Y;' to 'using Y = X::Y;'.
Diag(SS.getBeginLoc(), diag::note_using_decl_class_member_workaround)
<< 0 // alias declaration
<< FixItHint::CreateInsertion(SS.getBeginLoc(),
NameInfo.getName().getAsString() +
" = ");
<< diag::MemClassWorkaround::AliasDecl
<< FixItHint::CreateInsertion(SS.getBeginLoc(),
NameInfo.getName().getAsString() +
" = ");
} else {
// Convert 'using X::Y;' to 'typedef X::Y Y;'.
SourceLocation InsertLoc = getLocForEndOfToken(NameInfo.getEndLoc());
Diag(InsertLoc, diag::note_using_decl_class_member_workaround)
<< 1 // typedef declaration
<< FixItHint::CreateReplacement(UsingLoc, "typedef")
<< FixItHint::CreateInsertion(
InsertLoc, " " + NameInfo.getName().getAsString());
<< diag::MemClassWorkaround::TypedefDecl
<< FixItHint::CreateReplacement(UsingLoc, "typedef")
<< FixItHint::CreateInsertion(
InsertLoc, " " + NameInfo.getName().getAsString());
}
} else if (R->getAsSingle<VarDecl>()) {
// Don't provide a fixit outside C++11 mode; we don't want to suggest
Expand All @@ -13241,8 +13241,7 @@ bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, bool HasTypename,
}

Diag(UsingLoc, diag::note_using_decl_class_member_workaround)
<< 2 // reference declaration
<< FixIt;
<< diag::MemClassWorkaround::ReferenceDecl << FixIt;
} else if (R->getAsSingle<EnumConstantDecl>()) {
// Don't provide a fixit outside C++11 mode; we don't want to suggest
// repeating the type of the enumeration here, and we can't do so if
Expand All @@ -13256,8 +13255,10 @@ bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, bool HasTypename,
}

Diag(UsingLoc, diag::note_using_decl_class_member_workaround)
<< (getLangOpts().CPlusPlus11 ? 4 : 3) // const[expr] variable
<< FixIt;
<< (getLangOpts().CPlusPlus11
? diag::MemClassWorkaround::ConstexprVar
: diag::MemClassWorkaround::ConstVar)
<< FixIt;
}
}

Expand Down
45 changes: 45 additions & 0 deletions clang/test/TableGen/select-enum-errors.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// RUN: not clang-tblgen --gen-clang-diags-enums -DERROR1 -I%S %s 2>&1 | FileCheck %s --check-prefixes=CHECK,C1
// RUN: not clang-tblgen --gen-clang-diags-enums -DERROR2 -I%S %s 2>&1 | FileCheck %s --check-prefixes=CHECK,C2
// RUN: not clang-tblgen --gen-clang-diags-enums -DERROR3 -I%S %s 2>&1 | FileCheck %s --check-prefixes=CHECK,C3
// RUN: not clang-tblgen --gen-clang-diags-enums -DERROR4 -I%S %s 2>&1 | FileCheck %s --check-prefixes=CHECK,C4
include "DiagnosticBase.inc"

// No real reason to diagnose these, the namespace generated as the
// 'enumeration' name will never conflict with the enumerator.
def EnumerationEnumeratorDupe : Error<"%enum_select<Matchy>{%Matchy{haha}}0">;

// Enumerator values aren't required, though this does seem kind of silly/not
// particularly useful?
def NoEnumerators : Error<"%enum_select<Name>{foo|bar|baz}0">;

def DupeNames1 : Error<"%enum_select<DupeName>{}0">;
def DupeNames2 : Error<"%enum_select<DupeName>{}0">;
// CHECK: error: Duplicate enumeration name 'DupeName'
// CHECK-NEXT: def DupeNames2
// CHECK: note: Previous diagnostic is here
// CHECK-NEXT: def DupeNames1

def DupeValue : Error<"%enum_select<DupeValue>{%DName{V1}|%DName{V2}}0">;
// CHECK: error: Duplicate enumerator name 'DName'

#ifdef ERROR1
def EnumValNotExpected : Error<"%enum_select{V1|%Val2{V2}}0">;
// C1: expected '<' after enum_select
#endif

#ifdef ERROR2
def SelectWithArrow : Error<"%select<Something>{V1|%Val2{V2}}0">;
// C2: modifier '<' syntax not valid with %select
#endif

#ifdef ERROR3
// Missing closing > after the name of the enumeration
def MissingClosing : Error<"%enum_select<MissingClosingName{}0">;
// C3: expected '>' while parsing %enum_select
#endif

#ifdef ERROR4
// Missing { after the name of an enumerator
def MissingTextAfterEnumerator: Error<"%enum_select<Name>{%OtherName|foo}0">;
// C4: expected '{' while parsing %enum_select
#endif
Loading
Loading