aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Sema/SemaChecking.cpp
diff options
context:
space:
mode:
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)) {