From: Matt Beaumont-Gay Date: Fri, 6 Jan 2012 22:43:58 +0000 (+0000) Subject: Suppress -Wunused-value within macros from system headers. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57;p=clang Suppress -Wunused-value within macros from system headers. Along the way, move a helper function from SemaChecking.cpp to a more accessible home in SourceManager. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@147692 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/SourceManager.h b/include/clang/Basic/SourceManager.h index b1fb78ea56..038b8c65d0 100644 --- a/include/clang/Basic/SourceManager.h +++ b/include/clang/Basic/SourceManager.h @@ -1087,6 +1087,11 @@ public: return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem; } + /// \brief Returns whether \p Loc is expanded from a macro in a system header. + bool isInSystemMacro(SourceLocation loc) { + return loc.isMacroID() && isInSystemHeader(getSpellingLoc(loc)); + } + /// \brief The size of the SLocEnty that \p FID represents. unsigned getFileIDSize(FileID FID) const; diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 248998ed0b..73ff49a338 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -3755,11 +3755,6 @@ std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) { return ValueInRange.toString(10); } -static bool isFromSystemMacro(Sema &S, SourceLocation loc) { - SourceManager &smgr = S.Context.getSourceManager(); - return loc.isMacroID() && smgr.isInSystemHeader(smgr.getSpellingLoc(loc)); -} - void CheckImplicitConversion(Sema &S, Expr *E, QualType T, SourceLocation CC, bool *ICContext = 0) { if (E->isTypeDependent() || E->isValueDependent()) return; @@ -3821,7 +3816,7 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T, // Strip vector types. if (isa(Source)) { if (!isa(Target)) { - if (isFromSystemMacro(S, CC)) + if (S.SourceMgr.isInSystemMacro(CC)) return; return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar); } @@ -3838,7 +3833,7 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T, // Strip complex types. if (isa(Source)) { if (!isa(Target)) { - if (isFromSystemMacro(S, CC)) + if (S.SourceMgr.isInSystemMacro(CC)) return; return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar); @@ -3870,7 +3865,7 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T, return; } - if (isFromSystemMacro(S, CC)) + if (S.SourceMgr.isInSystemMacro(CC)) return; DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision); @@ -3880,7 +3875,7 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T, // If the target is integral, always warn. if ((TargetBT && TargetBT->isInteger())) { - if (isFromSystemMacro(S, CC)) + if (S.SourceMgr.isInSystemMacro(CC)) return; Expr *InnerE = E->IgnoreParenImpCasts(); @@ -3917,7 +3912,7 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T, // TODO: this should happen for bitfield stores, too. llvm::APSInt Value(32); if (E->isIntegerConstantExpr(Value, S.Context)) { - if (isFromSystemMacro(S, CC)) + if (S.SourceMgr.isInSystemMacro(CC)) return; std::string PrettySourceValue = Value.toString(10); @@ -3932,7 +3927,7 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T, } // People want to build with -Wshorten-64-to-32 and not -Wconversion. - if (isFromSystemMacro(S, CC)) + if (S.SourceMgr.isInSystemMacro(CC)) return; if (SourceRange.Width == 64 && TargetRange.Width == 32) @@ -3944,7 +3939,7 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T, (!TargetRange.NonNegative && SourceRange.NonNegative && SourceRange.Width == TargetRange.Width)) { - if (isFromSystemMacro(S, CC)) + if (S.SourceMgr.isInSystemMacro(CC)) return; unsigned DiagID = diag::warn_impcast_integer_sign; @@ -3982,7 +3977,7 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T, (TargetEnum->getDecl()->getIdentifier() || TargetEnum->getDecl()->getTypedefNameForAnonDecl()) && SourceEnum != TargetEnum) { - if (isFromSystemMacro(S, CC)) + if (S.SourceMgr.isInSystemMacro(CC)) return; return DiagnoseImpCast(S, E, SourceType, T, CC, diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp index d7489d418e..78a58964db 100644 --- a/lib/Sema/SemaStmt.cpp +++ b/lib/Sema/SemaStmt.cpp @@ -152,7 +152,8 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S) { SourceLocation Loc; SourceRange R1, R2; - if (!E->isUnusedResultAWarning(Loc, R1, R2, Context)) + if (SourceMgr.isInSystemMacro(E->getExprLoc()) || + !E->isUnusedResultAWarning(Loc, R1, R2, Context)) return; // Okay, we have an unused result. Depending on what the base expression is, diff --git a/test/Sema/Inputs/unused-expr-system-header.h b/test/Sema/Inputs/unused-expr-system-header.h new file mode 100644 index 0000000000..72b2d95de4 --- /dev/null +++ b/test/Sema/Inputs/unused-expr-system-header.h @@ -0,0 +1,23 @@ +// "System header" for testing that -Wunused-value is properly suppressed in +// certain cases. + +#define POSSIBLY_BAD_MACRO(x) \ + { int i = x; \ + i; } + +#define STATEMENT_EXPR_MACRO(x) \ + (__extension__ \ + ({int i = x; \ + i;})) + +#define COMMA_MACRO_1(x, y) \ + {x, y;} + +#define COMMA_MACRO_2(x, y) \ + if (x) { 1 == 2, y; } + +#define COMMA_MACRO_3(x, y) \ + (x, y) + +#define COMMA_MACRO_4(x, y) \ + ( 1 == 2, y ) diff --git a/test/Sema/unused-expr-system-header.c b/test/Sema/unused-expr-system-header.c new file mode 100644 index 0000000000..dcc8918970 --- /dev/null +++ b/test/Sema/unused-expr-system-header.c @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -isystem %S/Inputs -fsyntax-only -verify %s +#include +void f(int i1, int i2) { + POSSIBLY_BAD_MACRO(5); + STATEMENT_EXPR_MACRO(5); + COMMA_MACRO_1(i1 == i2, f(i1, i2)); // expected-warning {{expression result unused}} + COMMA_MACRO_2(i1 == i2, f(i1, i2)); + COMMA_MACRO_3(i1 == i2, f(i1, i2)); // expected-warning {{expression result unused}} + COMMA_MACRO_4(i1 == i2, f(i1, i2)); +}