aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Sema/SemaChecking.cpp
diff options
context:
space:
mode:
authorAlex Brachet <abrachet@google.com>2023-07-14 16:23:22 +0000
committerAlex Brachet <abrachet@google.com>2023-07-14 16:23:22 +0000
commit563a23c824db22da3ea378e8179fcc7072e897ec (patch)
treed427f2fa8df4c9822dbafc2e01d761ae583854b3 /clang/lib/Sema/SemaChecking.cpp
parent3c0a136ce4b724221a7f75b656b9f7af3de6b64c (diff)
downloadllvm-563a23c824db22da3ea378e8179fcc7072e897ec.zip
llvm-563a23c824db22da3ea378e8179fcc7072e897ec.tar.gz
llvm-563a23c824db22da3ea378e8179fcc7072e897ec.tar.bz2
[clang][Sema] Add fixit for scoped enum format error
This helps transition code bases to handle the new warning added in 3632e2f5179 Before: ``` clang/test/FixIt/format.cpp:10:16: warning: format specifies type 'int' but the argument has type 'N::E' [-Wformat] 10 | printf("%d", N::E::One); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}} | ~~ ^~~~~~~~~ | %d ``` After: ``` clang/test/FixIt/format.cpp:10:16: warning: format specifies type 'int' but the argument has type 'N::E' [-Wformat] 10 | printf("%d", N::E::One); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}} | ~~ ^~~~~~~~~ | static_cast<int>( ) ``` Differential Revision: https://reviews.llvm.org/D153623
Diffstat (limited to 'clang/lib/Sema/SemaChecking.cpp')
-rw-r--r--clang/lib/Sema/SemaChecking.cpp15
1 files changed, 12 insertions, 3 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 66a1a8f..b09d038 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -11077,12 +11077,15 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
assert(Match != ArgType::MatchPromotion);
// Look through unscoped enums to their underlying type.
bool IsEnum = false;
+ bool IsScopedEnum = false;
if (auto EnumTy = ExprTy->getAs<EnumType>()) {
if (EnumTy->isUnscopedEnumerationType()) {
ExprTy = EnumTy->getDecl()->getIntegerType();
// This controls whether we're talking about the underlying type or not,
// which we only want to do when it's an unscoped enum.
IsEnum = true;
+ } else {
+ IsScopedEnum = true;
}
}
@@ -11148,7 +11151,7 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen);
- if (IntendedTy == ExprTy && !ShouldNotPrintDirectly) {
+ if (IntendedTy == ExprTy && !ShouldNotPrintDirectly && !IsScopedEnum) {
unsigned Diag;
switch (Match) {
case ArgType::Match:
@@ -11185,11 +11188,17 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
SmallString<16> CastBuf;
llvm::raw_svector_ostream CastFix(CastBuf);
CastFix << (S.LangOpts.CPlusPlus ? "static_cast<" : "(");
- IntendedTy.print(CastFix, S.Context.getPrintingPolicy());
+ if (IsScopedEnum) {
+ CastFix << AT.getRepresentativeType(S.Context).getAsString(
+ S.Context.getPrintingPolicy());
+ } else {
+ IntendedTy.print(CastFix, S.Context.getPrintingPolicy());
+ }
CastFix << (S.LangOpts.CPlusPlus ? ">" : ")");
SmallVector<FixItHint,4> Hints;
- if (!AT.matchesType(S.Context, IntendedTy) || ShouldNotPrintDirectly)
+ if ((!AT.matchesType(S.Context, IntendedTy) && !IsScopedEnum) ||
+ ShouldNotPrintDirectly)
Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str()));
if (const CStyleCastExpr *CCast = dyn_cast<CStyleCastExpr>(E)) {