-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[Sema] Clean up -Wc++11-narrowing-const-reference code after #76094. NFC #77278
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
MaskRay
merged 1 commit into
main
from
users/MaskRay/spr/sema-clean-up-wc11-narrowing-const-reference-code-after-76094-nfc
Jan 8, 2024
Merged
[Sema] Clean up -Wc++11-narrowing-const-reference code after #76094. NFC #77278
MaskRay
merged 1 commit into
main
from
users/MaskRay/spr/sema-clean-up-wc11-narrowing-const-reference-code-after-76094-nfc
Jan 8, 2024
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Created using spr 1.3.4
@llvm/pr-subscribers-clang Author: Fangrui Song (MaskRay) ChangesFull diff: https://github.com/llvm/llvm-project/pull/77278.diff 2 Files Affected:
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index d150e08d5f5ea8..a97182cad5d513 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -6160,23 +6160,19 @@ def err_illegal_initializer_type : Error<"illegal initializer type %0">;
def ext_init_list_type_narrowing : ExtWarn<
"type %0 cannot be narrowed to %1 in initializer list">,
InGroup<CXX11Narrowing>, DefaultError, SFINAEFailure;
-// *_narrowing_const_reference diagnostics have the same messages, but are
-// controlled by -Wc++11-narrowing-const-reference for narrowing involving a
-// const reference.
def ext_init_list_type_narrowing_const_reference : ExtWarn<
- "type %0 cannot be narrowed to %1 in initializer list">,
+ ext_init_list_type_narrowing.Summary>,
InGroup<CXX11NarrowingConstReference>, DefaultError, SFINAEFailure;
def ext_init_list_variable_narrowing : ExtWarn<
"non-constant-expression cannot be narrowed from type %0 to %1 in "
"initializer list">, InGroup<CXX11Narrowing>, DefaultError, SFINAEFailure;
def ext_init_list_variable_narrowing_const_reference : ExtWarn<
- "non-constant-expression cannot be narrowed from type %0 to %1 in "
- "initializer list">, InGroup<CXX11NarrowingConstReference>, DefaultError, SFINAEFailure;
+ ext_init_list_variable_narrowing.Summary>, InGroup<CXX11NarrowingConstReference>, DefaultError, SFINAEFailure;
def ext_init_list_constant_narrowing : ExtWarn<
"constant expression evaluates to %0 which cannot be narrowed to type %1">,
InGroup<CXX11Narrowing>, DefaultError, SFINAEFailure;
def ext_init_list_constant_narrowing_const_reference : ExtWarn<
- "constant expression evaluates to %0 which cannot be narrowed to type %1">,
+ ext_init_list_constant_narrowing.Summary>,
InGroup<CXX11NarrowingConstReference>, DefaultError, SFINAEFailure;
def warn_init_list_type_narrowing : Warning<
"type %0 cannot be narrowed to %1 in initializer list in C++11">,
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index e469e420f14f03..408ee5f775804b 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -10377,11 +10377,6 @@ void InitializationSequence::dump() const {
dump(llvm::errs());
}
-static bool NarrowingErrs(const LangOptions &L) {
- return L.CPlusPlus11 &&
- (!L.MicrosoftExt || L.isCompatibleWithMSVC(LangOptions::MSVC2015));
-}
-
static void DiagnoseNarrowingInInitList(Sema &S,
const ImplicitConversionSequence &ICS,
QualType PreNarrowingType,
@@ -10402,6 +10397,19 @@ static void DiagnoseNarrowingInInitList(Sema &S,
return;
}
+ auto MakeDiag = [&](bool IsConstRef, unsigned DefaultDiagID,
+ unsigned ConstRefDiagID, unsigned WarnDiagID) {
+ unsigned DiagID;
+ auto &L = S.getLangOpts();
+ if (L.CPlusPlus11 &&
+ (!L.MicrosoftExt || L.isCompatibleWithMSVC(LangOptions::MSVC2015)))
+ DiagID = IsConstRef ? ConstRefDiagID : DefaultDiagID;
+ else
+ DiagID = WarnDiagID;
+ return S.Diag(PostInit->getBeginLoc(), DiagID)
+ << PostInit->getSourceRange();
+ };
+
// C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion.
APValue ConstantValue;
QualType ConstantType;
@@ -10417,13 +10425,9 @@ static void DiagnoseNarrowingInInitList(Sema &S,
// narrowing conversion even if the value is a constant and can be
// represented exactly as an integer.
QualType T = EntityType.getNonReferenceType();
- S.Diag(PostInit->getBeginLoc(),
- NarrowingErrs(S.getLangOpts())
- ? (T == EntityType
- ? diag::ext_init_list_type_narrowing
- : diag::ext_init_list_type_narrowing_const_reference)
- : diag::warn_init_list_type_narrowing)
- << PostInit->getSourceRange()
+ MakeDiag(T != EntityType, diag::ext_init_list_type_narrowing,
+ diag::ext_init_list_type_narrowing_const_reference,
+ diag::warn_init_list_type_narrowing)
<< PreNarrowingType.getLocalUnqualifiedType()
<< T.getLocalUnqualifiedType();
break;
@@ -10431,14 +10435,10 @@ static void DiagnoseNarrowingInInitList(Sema &S,
case NK_Constant_Narrowing: {
// A constant value was narrowed.
- QualType T = EntityType.getNonReferenceType();
- S.Diag(PostInit->getBeginLoc(),
- NarrowingErrs(S.getLangOpts())
- ? (T == EntityType
- ? diag::ext_init_list_constant_narrowing
- : diag::ext_init_list_constant_narrowing_const_reference)
- : diag::warn_init_list_constant_narrowing)
- << PostInit->getSourceRange()
+ MakeDiag(EntityType.getNonReferenceType() != EntityType,
+ diag::ext_init_list_constant_narrowing,
+ diag::ext_init_list_constant_narrowing_const_reference,
+ diag::warn_init_list_constant_narrowing)
<< ConstantValue.getAsString(S.getASTContext(), ConstantType)
<< EntityType.getNonReferenceType().getLocalUnqualifiedType();
break;
@@ -10446,14 +10446,10 @@ static void DiagnoseNarrowingInInitList(Sema &S,
case NK_Variable_Narrowing: {
// A variable's value may have been narrowed.
- QualType T = EntityType.getNonReferenceType();
- S.Diag(PostInit->getBeginLoc(),
- NarrowingErrs(S.getLangOpts())
- ? (T == EntityType
- ? diag::ext_init_list_variable_narrowing
- : diag::ext_init_list_variable_narrowing_const_reference)
- : diag::warn_init_list_variable_narrowing)
- << PostInit->getSourceRange()
+ MakeDiag(EntityType.getNonReferenceType() != EntityType,
+ diag::ext_init_list_variable_narrowing,
+ diag::ext_init_list_variable_narrowing_const_reference,
+ diag::warn_init_list_variable_narrowing)
<< PreNarrowingType.getLocalUnqualifiedType()
<< EntityType.getNonReferenceType().getLocalUnqualifiedType();
break;
|
erichkeane
approved these changes
Jan 8, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for this!
justinfargnoli
pushed a commit
to justinfargnoli/llvm-project
that referenced
this pull request
Jan 28, 2024
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.