diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td --- a/clang/include/clang/Basic/DiagnosticGroups.td +++ b/clang/include/clang/Basic/DiagnosticGroups.td @@ -724,6 +724,7 @@ def UnusedLabel : DiagGroup<"unused-label">; def UnusedLambdaCapture : DiagGroup<"unused-lambda-capture">; def UnusedParameter : DiagGroup<"unused-parameter">; +def UnusedButSetParameter : DiagGroup<"unused-but-set-parameter">; def UnusedResult : DiagGroup<"unused-result">; def PotentiallyEvaluatedExpression : DiagGroup<"potentially-evaluated-expression">; def UnevaluatedExpression : DiagGroup<"unevaluated-expression", @@ -733,6 +734,7 @@ def UnusedConstVariable : DiagGroup<"unused-const-variable">; def UnusedVariable : DiagGroup<"unused-variable", [UnusedConstVariable]>; +def UnusedButSetVariable : DiagGroup<"unused-but-set-variable">; def UnusedLocalTypedef : DiagGroup<"unused-local-typedef">; def UnusedPropertyIvar : DiagGroup<"unused-property-ivar">; def UnusedGetterReturnValue : DiagGroup<"unused-getter-return-value">; @@ -871,7 +873,7 @@ // UnusedMemberFunction, (clean-up llvm before enabling) UnusedPrivateField, UnusedLambdaCapture, UnusedLocalTypedef, UnusedValue, UnusedVariable, - UnusedPropertyIvar]>, + UnusedButSetVariable, UnusedPropertyIvar]>, DiagCategory<"Unused Entity Issue">; // Format settings. @@ -923,6 +925,7 @@ MissingMethodReturnType, SignCompare, UnusedParameter, + UnusedButSetParameter, NullPointerArithmetic, EmptyInitStatement, StringConcatation, diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -310,8 +310,12 @@ "repeated RISC-V 'interrupt' attribute is here">; def warn_unused_parameter : Warning<"unused parameter %0">, InGroup, DefaultIgnore; +def warn_unused_but_set_parameter : Warning<"parameter %0 set but not used">, + InGroup, DefaultIgnore; def warn_unused_variable : Warning<"unused variable %0">, InGroup, DefaultIgnore; +def warn_unused_but_set_variable : Warning<"variable %0 set but not used">, + InGroup, DefaultIgnore; def warn_unused_local_typedef : Warning< "unused %select{typedef|type alias}0 %1">, InGroup, DefaultIgnore; diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -1514,6 +1514,11 @@ bool WarnedStackExhausted = false; + /// Increment when we find a reference; decrement when we find an ignored + /// assignment. Ultimately the value is 0 if every reference is an ignored + /// assignment. + llvm::DenseMap RefsMinusAssignments; + public: Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer, TranslationUnitKind TUKind = TU_Complete, @@ -4856,6 +4861,10 @@ void DiagnoseUnusedNestedTypedefs(const RecordDecl *D); void DiagnoseUnusedDecl(const NamedDecl *ND); + /// If VD is set but not otherwise used, diagnose, for a parameter or a + /// variable. + void DiagnoseUnusedButSetDecl(const VarDecl *VD); + /// Emit \p DiagID if statement located on \p StmtLoc has a suspicious null /// statement as a \p Body, and it is located on the same line. /// diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -1905,6 +1905,37 @@ Diag(D->getLocation(), DiagID) << D << Hint; } +void Sema::DiagnoseUnusedButSetDecl(const VarDecl *VD) { + // If it's not referenced, it can't be set. + if (!VD->isReferenced() || !VD->getDeclName() || VD->hasAttr()) + return; + + const auto *Ty = VD->getType().getTypePtr()->getBaseElementTypeUnsafe(); + if (const TagType *TT = Ty->getAs()) { + const TagDecl *Tag = TT->getDecl(); + if (Tag->hasAttr()) + return; + // In C++, don't warn for record types that don't have WarnUnusedAttr, to + // mimic gcc's behavior. + if (const CXXRecordDecl *RD = dyn_cast(Tag)) { + if (!RD->hasAttr()) + return; + } + } + + auto iter = RefsMinusAssignments.find(VD); + if (iter == RefsMinusAssignments.end()) + return; + + assert(iter->getSecond() >= 0 && + "Found a negative number of references to a VarDecl"); + if (iter->getSecond() != 0) + return; + unsigned DiagID = isa(VD) ? diag::warn_unused_but_set_parameter + : diag::warn_unused_but_set_variable; + Diag(VD->getLocation(), DiagID) << VD; +} + static void CheckPoppedLabel(LabelDecl *L, Sema &S) { // Verify that we have no forward references left. If so, there was a goto // or address of a label taken, but no definition of it. Label fwd @@ -1939,6 +1970,11 @@ DiagnoseUnusedNestedTypedefs(RD); } + if (VarDecl *VD = dyn_cast(D)) { + DiagnoseUnusedButSetDecl(VD); + RefsMinusAssignments.erase(VD); + } + if (!D->getDeclName()) continue; // If this was a forward reference to a label, verify it was defined. diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -18303,8 +18303,9 @@ "MarkVarDeclODRUsed failed to cleanup MaybeODRUseExprs?"); } -static void DoMarkVarDeclReferenced(Sema &SemaRef, SourceLocation Loc, - VarDecl *Var, Expr *E) { +static void DoMarkVarDeclReferenced( + Sema &SemaRef, SourceLocation Loc, VarDecl *Var, Expr *E, + llvm::DenseMap &RefsMinusAssignments) { assert((!E || isa(E) || isa(E) || isa(E)) && "Invalid Expr argument to DoMarkVarDeclReferenced"); @@ -18339,6 +18340,10 @@ bool UsableInConstantExpr = Var->mightBeUsableInConstantExpressions(SemaRef.Context); + if (Var->isLocalVarDeclOrParm() && !Var->hasExternalStorage()) { + RefsMinusAssignments.insert({Var, 0}).first->getSecond()++; + } + // C++20 [expr.const]p12: // A variable [...] is needed for constant evaluation if it is [...] a // variable whose name appears as a potentially constant evaluated @@ -18494,16 +18499,18 @@ /// (C++ [basic.def.odr]p2, C99 6.9p3). Note that this should not be /// used directly for normal expressions referring to VarDecl. void Sema::MarkVariableReferenced(SourceLocation Loc, VarDecl *Var) { - DoMarkVarDeclReferenced(*this, Loc, Var, nullptr); + DoMarkVarDeclReferenced(*this, Loc, Var, nullptr, RefsMinusAssignments); } -static void MarkExprReferenced(Sema &SemaRef, SourceLocation Loc, - Decl *D, Expr *E, bool MightBeOdrUse) { +static void +MarkExprReferenced(Sema &SemaRef, SourceLocation Loc, Decl *D, Expr *E, + bool MightBeOdrUse, + llvm::DenseMap &RefsMinusAssignments) { if (SemaRef.isInOpenMPDeclareTargetContext()) SemaRef.checkDeclIsAllowedInOpenMPTarget(E, D); if (VarDecl *Var = dyn_cast(D)) { - DoMarkVarDeclReferenced(SemaRef, Loc, Var, E); + DoMarkVarDeclReferenced(SemaRef, Loc, Var, E, RefsMinusAssignments); return; } @@ -18549,7 +18556,8 @@ if (!isConstantEvaluated() && FD->isConsteval() && !RebuildingImmediateInvocation) ExprEvalContexts.back().ReferenceToConsteval.insert(E); - MarkExprReferenced(*this, E->getLocation(), E->getDecl(), E, OdrUse); + MarkExprReferenced(*this, E->getLocation(), E->getDecl(), E, OdrUse, + RefsMinusAssignments); } /// Perform reference-marking and odr-use handling for a MemberExpr. @@ -18568,13 +18576,15 @@ } SourceLocation Loc = E->getMemberLoc().isValid() ? E->getMemberLoc() : E->getBeginLoc(); - MarkExprReferenced(*this, Loc, E->getMemberDecl(), E, MightBeOdrUse); + MarkExprReferenced(*this, Loc, E->getMemberDecl(), E, MightBeOdrUse, + RefsMinusAssignments); } /// Perform reference-marking and odr-use handling for a FunctionParmPackExpr. void Sema::MarkFunctionParmPackReferenced(FunctionParmPackExpr *E) { for (VarDecl *VD : *E) - MarkExprReferenced(*this, E->getParameterPackLocation(), VD, E, true); + MarkExprReferenced(*this, E->getParameterPackLocation(), VD, E, true, + RefsMinusAssignments); } /// Perform marking for a reference to an arbitrary declaration. It diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -7782,9 +7782,34 @@ return BuildCXXNoexceptExpr(KeyLoc, Operand, RParen); } +static void MaybeDecrementCount( + Expr *E, llvm::DenseMap &RefsMinusAssignments) { + DeclRefExpr *LHS = nullptr; + if (BinaryOperator *BO = dyn_cast(E)) { + if (!BO->isAssignmentOp()) + return; + LHS = dyn_cast(BO->getLHS()); + } else if (CXXOperatorCallExpr *COCE = dyn_cast(E)) { + if (!COCE->isAssignmentOp()) + return; + LHS = dyn_cast(COCE->getArg(0)); + } + if (!LHS) + return; + VarDecl *VD = dyn_cast(LHS->getDecl()); + if (!VD) + return; + auto iter = RefsMinusAssignments.find(VD); + if (iter == RefsMinusAssignments.end()) + return; + iter->getSecond()--; +} + /// Perform the conversions required for an expression used in a /// context that ignores the result. ExprResult Sema::IgnoredValueConversions(Expr *E) { + MaybeDecrementCount(E, RefsMinusAssignments); + if (E->hasPlaceholderType()) { ExprResult result = CheckPlaceholderExpr(E); if (result.isInvalid()) return E; diff --git a/clang/test/CXX/expr/expr.prim/expr.prim.lambda/p12.cpp b/clang/test/CXX/expr/expr.prim/expr.prim.lambda/p12.cpp --- a/clang/test/CXX/expr/expr.prim/expr.prim.lambda/p12.cpp +++ b/clang/test/CXX/expr/expr.prim/expr.prim.lambda/p12.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -std=c++11 %s -Wunused -Wno-unused-lambda-capture -verify +// RUN: %clang_cc1 -std=c++11 %s -Wunused -Wno-unused-but-set-variable -Wno-unused-lambda-capture -verify void odr_used() { int i = 17; diff --git a/clang/test/CodeGen/2007-10-30-Volatile.c b/clang/test/CodeGen/2007-10-30-Volatile.c --- a/clang/test/CodeGen/2007-10-30-Volatile.c +++ b/clang/test/CodeGen/2007-10-30-Volatile.c @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -emit-llvm %s -o /dev/null -Wall -Werror +// RUN: %clang_cc1 -emit-llvm %s -o /dev/null -Wall -Wno-unused-but-set-variable -Werror void bork() { char * volatile p = 0; volatile int cc = 0; diff --git a/clang/test/CodeGen/X86/x86_32-xsave.c b/clang/test/CodeGen/X86/x86_32-xsave.c --- a/clang/test/CodeGen/X86/x86_32-xsave.c +++ b/clang/test/CodeGen/X86/x86_32-xsave.c @@ -1,17 +1,17 @@ -// RUN: %clang_cc1 %s -DTEST_XSAVE -O0 -triple=i686-unknown-unknown -target-feature +xsave -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefix=XSAVE -// RUN: %clang_cc1 %s -DTEST_XSAVE -O0 -triple=i686-unknown-unknown -target-feature +xsave -fno-signed-char -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefix=XSAVE +// RUN: %clang_cc1 %s -DTEST_XSAVE -O0 -triple=i686-unknown-unknown -target-feature +xsave -emit-llvm -o - -Wall -Wno-unused-but-set-variable -Werror | FileCheck %s --check-prefix=XSAVE +// RUN: %clang_cc1 %s -DTEST_XSAVE -O0 -triple=i686-unknown-unknown -target-feature +xsave -fno-signed-char -emit-llvm -o - -Wall -Wno-unused-but-set-variable -Werror | FileCheck %s --check-prefix=XSAVE -// RUN: %clang_cc1 %s -DTEST_XGETBV -O0 -triple=i686-unknown-unknown -target-feature +xsave -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefix=XGETBV -// RUN: %clang_cc1 %s -DTEST_XSETBV -O0 -triple=i686-unknown-unknown -target-feature +xsave -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefix=XSETBV +// RUN: %clang_cc1 %s -DTEST_XGETBV -O0 -triple=i686-unknown-unknown -target-feature +xsave -emit-llvm -o - -Wall -Wno-unused-but-set-variable -Werror | FileCheck %s --check-prefix=XGETBV +// RUN: %clang_cc1 %s -DTEST_XSETBV -O0 -triple=i686-unknown-unknown -target-feature +xsave -emit-llvm -o - -Wall -Wno-unused-but-set-variable -Werror | FileCheck %s --check-prefix=XSETBV -// RUN: %clang_cc1 %s -DTEST_XSAVEOPT -O0 -triple=i686-unknown-unknown -target-feature +xsave -target-feature +xsaveopt -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefix=XSAVEOPT -// RUN: %clang_cc1 %s -DTEST_XSAVEOPT -O0 -triple=i686-unknown-unknown -target-feature +xsave -target-feature +xsaveopt -fno-signed-char -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefix=XSAVEOPT +// RUN: %clang_cc1 %s -DTEST_XSAVEOPT -O0 -triple=i686-unknown-unknown -target-feature +xsave -target-feature +xsaveopt -emit-llvm -o - -Wall -Wno-unused-but-set-variable -Werror | FileCheck %s --check-prefix=XSAVEOPT +// RUN: %clang_cc1 %s -DTEST_XSAVEOPT -O0 -triple=i686-unknown-unknown -target-feature +xsave -target-feature +xsaveopt -fno-signed-char -emit-llvm -o - -Wall -Wno-unused-but-set-variable -Werror | FileCheck %s --check-prefix=XSAVEOPT -// RUN: %clang_cc1 %s -DTEST_XSAVEC -O0 -triple=i686-unknown-unknown -target-feature +xsave -target-feature +xsavec -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefix=XSAVEC -// RUN: %clang_cc1 %s -DTEST_XSAVEC -O0 -triple=i686-unknown-unknown -target-feature +xsave -target-feature +xsavec -fno-signed-char -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefix=XSAVEC +// RUN: %clang_cc1 %s -DTEST_XSAVEC -O0 -triple=i686-unknown-unknown -target-feature +xsave -target-feature +xsavec -emit-llvm -o - -Wall -Wno-unused-but-set-variable -Werror | FileCheck %s --check-prefix=XSAVEC +// RUN: %clang_cc1 %s -DTEST_XSAVEC -O0 -triple=i686-unknown-unknown -target-feature +xsave -target-feature +xsavec -fno-signed-char -emit-llvm -o - -Wall -Wno-unused-but-set-variable -Werror | FileCheck %s --check-prefix=XSAVEC -// RUN: %clang_cc1 %s -DTEST_XSAVES -O0 -triple=i686-unknown-unknown -target-feature +xsave -target-feature +xsaves -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefix=XSAVES -// RUN: %clang_cc1 %s -DTEST_XSAVES -O0 -triple=i686-unknown-unknown -target-feature +xsave -target-feature +xsaves -fno-signed-char -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefix=XSAVES +// RUN: %clang_cc1 %s -DTEST_XSAVES -O0 -triple=i686-unknown-unknown -target-feature +xsave -target-feature +xsaves -emit-llvm -o - -Wall -Wno-unused-but-set-variable -Werror | FileCheck %s --check-prefix=XSAVES +// RUN: %clang_cc1 %s -DTEST_XSAVES -O0 -triple=i686-unknown-unknown -target-feature +xsave -target-feature +xsaves -fno-signed-char -emit-llvm -o - -Wall -Wno-unused-but-set-variable -Werror | FileCheck %s --check-prefix=XSAVES // Don't include mm_malloc.h, it's system specific. #define __MM_MALLOC_H diff --git a/clang/test/CodeGen/X86/x86_64-xsave.c b/clang/test/CodeGen/X86/x86_64-xsave.c --- a/clang/test/CodeGen/X86/x86_64-xsave.c +++ b/clang/test/CodeGen/X86/x86_64-xsave.c @@ -1,17 +1,17 @@ -// RUN: %clang_cc1 %s -DTEST_XSAVE -O0 -triple=x86_64-unknown-unknown -target-feature +xsave -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefix=XSAVE -// RUN: %clang_cc1 %s -DTEST_XSAVE -O0 -triple=x86_64-unknown-unknown -target-feature +xsave -fno-signed-char -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefix=XSAVE +// RUN: %clang_cc1 %s -DTEST_XSAVE -O0 -triple=x86_64-unknown-unknown -target-feature +xsave -emit-llvm -o - -Wall -Wno-unused-but-set-variable -Werror | FileCheck %s --check-prefix=XSAVE +// RUN: %clang_cc1 %s -DTEST_XSAVE -O0 -triple=x86_64-unknown-unknown -target-feature +xsave -fno-signed-char -emit-llvm -o - -Wall -Wno-unused-but-set-variable -Werror | FileCheck %s --check-prefix=XSAVE -// RUN: %clang_cc1 %s -DTEST_XGETBV -O0 -triple=x86_64-unknown-unknown -target-feature +xsave -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefix=XGETBV -// RUN: %clang_cc1 %s -DTEST_XSETBV -O0 -triple=x86_64-unknown-unknown -target-feature +xsave -fno-signed-char -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefix=XSETBV +// RUN: %clang_cc1 %s -DTEST_XGETBV -O0 -triple=x86_64-unknown-unknown -target-feature +xsave -emit-llvm -o - -Wall -Wno-unused-but-set-variable -Werror | FileCheck %s --check-prefix=XGETBV +// RUN: %clang_cc1 %s -DTEST_XSETBV -O0 -triple=x86_64-unknown-unknown -target-feature +xsave -fno-signed-char -emit-llvm -o - -Wall -Wno-unused-but-set-variable -Werror | FileCheck %s --check-prefix=XSETBV -// RUN: %clang_cc1 %s -DTEST_XSAVEOPT -O0 -triple=x86_64-unknown-unknown -target-feature +xsave -target-feature +xsaveopt -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefix=XSAVEOPT -// RUN: %clang_cc1 %s -DTEST_XSAVEOPT -O0 -triple=x86_64-unknown-unknown -target-feature +xsave -target-feature +xsaveopt -fno-signed-char -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefix=XSAVEOPT +// RUN: %clang_cc1 %s -DTEST_XSAVEOPT -O0 -triple=x86_64-unknown-unknown -target-feature +xsave -target-feature +xsaveopt -emit-llvm -o - -Wall -Wno-unused-but-set-variable -Werror | FileCheck %s --check-prefix=XSAVEOPT +// RUN: %clang_cc1 %s -DTEST_XSAVEOPT -O0 -triple=x86_64-unknown-unknown -target-feature +xsave -target-feature +xsaveopt -fno-signed-char -emit-llvm -o - -Wall -Wno-unused-but-set-variable -Werror | FileCheck %s --check-prefix=XSAVEOPT -// RUN: %clang_cc1 %s -DTEST_XSAVEC -O0 -triple=x86_64-unknown-unknown -target-feature +xsave -target-feature +xsavec -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefix=XSAVEC -// RUN: %clang_cc1 %s -DTEST_XSAVEC -O0 -triple=x86_64-unknown-unknown -target-feature +xsave -target-feature +xsavec -fno-signed-char -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefix=XSAVEC +// RUN: %clang_cc1 %s -DTEST_XSAVEC -O0 -triple=x86_64-unknown-unknown -target-feature +xsave -target-feature +xsavec -emit-llvm -o - -Wall -Wno-unused-but-set-variable -Werror | FileCheck %s --check-prefix=XSAVEC +// RUN: %clang_cc1 %s -DTEST_XSAVEC -O0 -triple=x86_64-unknown-unknown -target-feature +xsave -target-feature +xsavec -fno-signed-char -emit-llvm -o - -Wall -Wno-unused-but-set-variable -Werror | FileCheck %s --check-prefix=XSAVEC -// RUN: %clang_cc1 %s -DTEST_XSAVES -O0 -triple=x86_64-unknown-unknown -target-feature +xsave -target-feature +xsaves -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefix=XSAVES -// RUN: %clang_cc1 %s -DTEST_XSAVES -O0 -triple=x86_64-unknown-unknown -target-feature +xsave -target-feature +xsaves -fno-signed-char -emit-llvm -o - -Wall -Werror | FileCheck %s --check-prefix=XSAVES +// RUN: %clang_cc1 %s -DTEST_XSAVES -O0 -triple=x86_64-unknown-unknown -target-feature +xsave -target-feature +xsaves -emit-llvm -o - -Wall -Wno-unused-but-set-variable -Werror | FileCheck %s --check-prefix=XSAVES +// RUN: %clang_cc1 %s -DTEST_XSAVES -O0 -triple=x86_64-unknown-unknown -target-feature +xsave -target-feature +xsaves -fno-signed-char -emit-llvm -o - -Wall -Wno-unused-but-set-variable -Werror | FileCheck %s --check-prefix=XSAVES // Don't include mm_malloc.h, it's system specific. #define __MM_MALLOC_H diff --git a/clang/test/CodeGen/builtins-arm.c b/clang/test/CodeGen/builtins-arm.c --- a/clang/test/CodeGen/builtins-arm.c +++ b/clang/test/CodeGen/builtins-arm.c @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -Wall -Werror -triple thumbv7-eabi -target-cpu cortex-a8 -disable-O0-optnone -emit-llvm -o - %s | opt -S -mem2reg | FileCheck %s +// RUN: %clang_cc1 -Wall -Wno-unused-but-set-variable -Werror -triple thumbv7-eabi -target-cpu cortex-a8 -disable-O0-optnone -emit-llvm -o - %s | opt -S -mem2reg | FileCheck %s #include diff --git a/clang/test/CodeGen/builtins-riscv.c b/clang/test/CodeGen/builtins-riscv.c --- a/clang/test/CodeGen/builtins-riscv.c +++ b/clang/test/CodeGen/builtins-riscv.c @@ -1,5 +1,5 @@ -// RUN: %clang_cc1 -Wall -Werror -triple riscv32 -disable-O0-optnone -emit-llvm -o - %s | opt -S -mem2reg | FileCheck %s -// RUN: %clang_cc1 -Wall -Werror -triple riscv64 -disable-O0-optnone -emit-llvm -o - %s | opt -S -mem2reg | FileCheck %s +// RUN: %clang_cc1 -Wall -Wno-unused-but-set-variable -Werror -triple riscv32 -disable-O0-optnone -emit-llvm -o - %s | opt -S -mem2reg | FileCheck %s +// RUN: %clang_cc1 -Wall -Wno-unused-but-set-variable -Werror -triple riscv64 -disable-O0-optnone -emit-llvm -o - %s | opt -S -mem2reg | FileCheck %s void test_eh_return_data_regno() { // CHECK: store volatile i32 10 diff --git a/clang/test/FixIt/fixit.cpp b/clang/test/FixIt/fixit.cpp --- a/clang/test/FixIt/fixit.cpp +++ b/clang/test/FixIt/fixit.cpp @@ -1,12 +1,12 @@ -// RUN: %clang_cc1 -pedantic -Wall -Wno-comment -verify -fcxx-exceptions -x c++ -std=c++98 %s +// RUN: %clang_cc1 -pedantic -Wall -Wno-unused-but-set-variable -Wno-comment -verify -fcxx-exceptions -x c++ -std=c++98 %s // RUN: cp %s %t-98 -// RUN: not %clang_cc1 -pedantic -Wall -Wno-comment -fcxx-exceptions -fixit -x c++ -std=c++98 %t-98 -// RUN: %clang_cc1 -fsyntax-only -pedantic -Wall -Werror -Wno-comment -fcxx-exceptions -x c++ -std=c++98 %t-98 +// RUN: not %clang_cc1 -pedantic -Wall -Wno-unused-but-set-variable -Wno-comment -fcxx-exceptions -fixit -x c++ -std=c++98 %t-98 +// RUN: %clang_cc1 -fsyntax-only -pedantic -Wall -Wno-unused-but-set-variable -Werror -Wno-comment -fcxx-exceptions -x c++ -std=c++98 %t-98 // RUN: not %clang_cc1 -fsyntax-only -pedantic -fdiagnostics-parseable-fixits -x c++ -std=c++11 %s 2>&1 | FileCheck %s -// RUN: %clang_cc1 -pedantic -Wall -Wno-comment -verify -fcxx-exceptions -x c++ -std=c++11 %s +// RUN: %clang_cc1 -pedantic -Wall -Wno-unused-but-set-variable -Wno-comment -verify -fcxx-exceptions -x c++ -std=c++11 %s // RUN: cp %s %t-11 -// RUN: not %clang_cc1 -pedantic -Wall -Wno-comment -fcxx-exceptions -fixit -x c++ -std=c++11 %t-11 -// RUN: %clang_cc1 -fsyntax-only -pedantic -Wall -Werror -Wno-comment -fcxx-exceptions -x c++ -std=c++11 %t-11 +// RUN: not %clang_cc1 -pedantic -Wall -Wno-unused-but-set-variable -Wno-comment -fcxx-exceptions -fixit -x c++ -std=c++11 %t-11 +// RUN: %clang_cc1 -fsyntax-only -pedantic -Wall -Wno-unused-but-set-variable -Werror -Wno-comment -fcxx-exceptions -x c++ -std=c++11 %t-11 /* This is a test of the various code modification hints that are provided as part of warning or extension diagnostics. All of the diff --git a/clang/test/Misc/warning-wall.c b/clang/test/Misc/warning-wall.c --- a/clang/test/Misc/warning-wall.c +++ b/clang/test/Misc/warning-wall.c @@ -73,6 +73,7 @@ CHECK-NEXT: -Wpotentially-evaluated-expression CHECK-NEXT: -Wunused-variable CHECK-NEXT: -Wunused-const-variable +CHECK-NEXT: -Wunused-but-set-variable CHECK-NEXT: -Wunused-property-ivar CHECK-NEXT: -Wvolatile-register-var CHECK-NEXT: -Wobjc-missing-super-calls diff --git a/clang/test/Sema/shift.c b/clang/test/Sema/shift.c --- a/clang/test/Sema/shift.c +++ b/clang/test/Sema/shift.c @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -Wall -Wshift-sign-overflow -ffreestanding -fsyntax-only -verify %s +// RUN: %clang_cc1 -Wall -Wno-unused-but-set-variable -Wshift-sign-overflow -ffreestanding -fsyntax-only -verify %s #include diff --git a/clang/test/Sema/vector-gcc-compat.c b/clang/test/Sema/vector-gcc-compat.c --- a/clang/test/Sema/vector-gcc-compat.c +++ b/clang/test/Sema/vector-gcc-compat.c @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 %s -verify -fsyntax-only -Weverything -triple x86_64-apple-darwin10 +// RUN: %clang_cc1 %s -verify -fsyntax-only -Weverything -Wno-unused-but-set-variable -triple x86_64-apple-darwin10 // Test the compatibility of clang's vector extensions with gcc's vector // extensions for C. Notably &&, ||, ?: and ! are not available. diff --git a/clang/test/Sema/vector-gcc-compat.cpp b/clang/test/Sema/vector-gcc-compat.cpp --- a/clang/test/Sema/vector-gcc-compat.cpp +++ b/clang/test/Sema/vector-gcc-compat.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 %s -verify -fsyntax-only -Weverything -std=c++11 -triple x86_64-apple-darwin10 +// RUN: %clang_cc1 %s -verify -fsyntax-only -Weverything -Wno-unused-but-set-variable -std=c++11 -triple x86_64-apple-darwin10 // Test the compatibility of clang++'s vector extensions with g++'s vector // extensions. In comparison to the extensions available in C, the !, ?:, && and diff --git a/clang/test/Sema/warn-unused-but-set-parameters.c b/clang/test/Sema/warn-unused-but-set-parameters.c new file mode 100644 --- /dev/null +++ b/clang/test/Sema/warn-unused-but-set-parameters.c @@ -0,0 +1,26 @@ +// RUN: %clang_cc1 -fblocks -fsyntax-only -Wunused-but-set-parameter -verify %s + +int f0(int x, + int y, // expected-warning{{parameter 'y' set but not used}} + int z __attribute__((unused))) { + y = 0; + return x; +} + +void f1(void) { + (void)^(int x, + int y, // expected-warning{{parameter 'y' set but not used}} + int z __attribute__((unused))) { + y = 0; + return x; + }; +} + +struct S { + int i; +}; + +void f3(struct S s) { // expected-warning{{parameter 's' set but not used}} + struct S t; + s = t; +} diff --git a/clang/test/Sema/warn-unused-but-set-variables.c b/clang/test/Sema/warn-unused-but-set-variables.c new file mode 100644 --- /dev/null +++ b/clang/test/Sema/warn-unused-but-set-variables.c @@ -0,0 +1,47 @@ +// RUN: %clang_cc1 -fblocks -fsyntax-only -Wunused-but-set-variable -verify %s + +struct S { + int i; +}; + +int f0() { + int y; // expected-warning{{variable 'y' set but not used}} + y = 0; + + int z __attribute__((unused)); + z = 0; + + struct S s; // expected-warning{{variable 's' set but not used}} + struct S t; + s = t; + + // Don't warn for an extern variable. + extern int w; + w = 0; + + // Following gcc, this should not warn. + int a; + w = (a = 0); + + int x; + x = 0; + return x; +} + +void f1(void) { + (void)^() { + int y; // expected-warning{{variable 'y' set but not used}} + y = 0; + + int x; + x = 0; + return x; + }; +} + +void f2 (void) { + // Don't warn, even if it's only used in a non-ODR context. + int x; + x = 0; + (void) sizeof(x); +} diff --git a/clang/test/SemaCXX/goto.cpp b/clang/test/SemaCXX/goto.cpp --- a/clang/test/SemaCXX/goto.cpp +++ b/clang/test/SemaCXX/goto.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -fsyntax-only -verify -Wall -fblocks %s +// RUN: %clang_cc1 -fsyntax-only -verify -Wall -Wno-unused-but-set-variable -fblocks %s // PR9463 double *end; diff --git a/clang/test/SemaCXX/shift.cpp b/clang/test/SemaCXX/shift.cpp --- a/clang/test/SemaCXX/shift.cpp +++ b/clang/test/SemaCXX/shift.cpp @@ -1,5 +1,5 @@ -// RUN: %clang_cc1 -Wall -Wshift-sign-overflow -ffreestanding -fsyntax-only -verify=expected,cxx17 -std=c++17 %s -// RUN: %clang_cc1 -Wall -Wshift-sign-overflow -ffreestanding -fsyntax-only -verify=expected,cxx2a -std=c++2a %s +// RUN: %clang_cc1 -Wall -Wno-unused-but-set-variable -Wshift-sign-overflow -ffreestanding -fsyntax-only -verify=expected,cxx17 -std=c++17 %s +// RUN: %clang_cc1 -Wall -Wno-unused-but-set-variable -Wshift-sign-overflow -ffreestanding -fsyntax-only -verify=expected,cxx2a -std=c++2a %s #include diff --git a/clang/test/SemaCXX/sizeless-1.cpp b/clang/test/SemaCXX/sizeless-1.cpp --- a/clang/test/SemaCXX/sizeless-1.cpp +++ b/clang/test/SemaCXX/sizeless-1.cpp @@ -1,7 +1,7 @@ -// RUN: %clang_cc1 -fcxx-exceptions -fsyntax-only -verify -W -Wall -Wrange-loop-analysis -triple arm64-linux-gnu -target-feature +sve -std=c++98 %s -// RUN: %clang_cc1 -fcxx-exceptions -fsyntax-only -verify -W -Wall -Wrange-loop-analysis -triple arm64-linux-gnu -target-feature +sve -std=c++11 %s -// RUN: %clang_cc1 -fcxx-exceptions -fsyntax-only -verify -W -Wall -Wrange-loop-analysis -triple arm64-linux-gnu -target-feature +sve -std=c++17 %s -// RUN: %clang_cc1 -fcxx-exceptions -fsyntax-only -verify -W -Wall -Wrange-loop-analysis -triple arm64-linux-gnu -target-feature +sve -std=gnu++17 %s +// RUN: %clang_cc1 -fcxx-exceptions -fsyntax-only -verify -W -Wall -Wno-unused-but-set-variable -Wrange-loop-analysis -triple arm64-linux-gnu -target-feature +sve -std=c++98 %s +// RUN: %clang_cc1 -fcxx-exceptions -fsyntax-only -verify -W -Wall -Wno-unused-but-set-variable -Wrange-loop-analysis -triple arm64-linux-gnu -target-feature +sve -std=c++11 %s +// RUN: %clang_cc1 -fcxx-exceptions -fsyntax-only -verify -W -Wall -Wno-unused-but-set-variable -Wrange-loop-analysis -triple arm64-linux-gnu -target-feature +sve -std=c++17 %s +// RUN: %clang_cc1 -fcxx-exceptions -fsyntax-only -verify -W -Wall -Wno-unused-but-set-variable -Wrange-loop-analysis -triple arm64-linux-gnu -target-feature +sve -std=gnu++17 %s namespace std { struct type_info; diff --git a/clang/test/SemaCXX/warn-unused-but-set-parameters-cpp.cpp b/clang/test/SemaCXX/warn-unused-but-set-parameters-cpp.cpp new file mode 100644 --- /dev/null +++ b/clang/test/SemaCXX/warn-unused-but-set-parameters-cpp.cpp @@ -0,0 +1,33 @@ +// RUN: %clang_cc1 -fblocks -fsyntax-only -Wunused-but-set-parameter -verify %s + +int f0(int x, + int y, // expected-warning{{parameter 'y' set but not used}} + int z __attribute__((unused))) { + y = 0; + return x; +} + +void f1(void) { + (void)^(int x, + int y, // expected-warning{{parameter 'y' set but not used}} + int z __attribute__((unused))) { + y = 0; + return x; + }; +} + +struct S { + int i; +}; + +// In C++, don't warn for a struct (following gcc). +void f3(struct S s) { + struct S t; + s = t; +} + +// Make sure this doesn't warn. +struct A { + int i; + A(int j) : i(j) {} +}; diff --git a/clang/test/SemaCXX/warn-unused-but-set-variables-cpp.cpp b/clang/test/SemaCXX/warn-unused-but-set-variables-cpp.cpp new file mode 100644 --- /dev/null +++ b/clang/test/SemaCXX/warn-unused-but-set-variables-cpp.cpp @@ -0,0 +1,50 @@ +// RUN: %clang_cc1 -fblocks -fsyntax-only -Wunused-but-set-variable -verify %s + +struct S { + int i; +}; + +struct __attribute__((warn_unused)) SWarnUnused { + int j; +}; + +int f0() { + int y; // expected-warning{{variable 'y' set but not used}} + y = 0; + + int z __attribute__((unused)); + z = 0; + + // In C++, don't warn for structs. (following gcc's behavior) + struct S s; + struct S t; + s = t; + + // Unless it's marked with the warn_unused attribute. + struct SWarnUnused swu; // expected-warning{{variable 'swu' set but not used}} + struct SWarnUnused swu2; + swu = swu2; + + int x; + x = 0; + return x + 5; +} + +void f1(void) { + (void)^() { + int y; // expected-warning{{variable 'y' set but not used}} + y = 0; + + int x; + x = 0; + return x; + }; +} + +void f2() { + // Don't warn for either of these cases. + constexpr int x = 2; + const int y = 1; + char a[x]; + char b[y]; +} diff --git a/clang/test/SemaObjC/foreach.m b/clang/test/SemaObjC/foreach.m --- a/clang/test/SemaObjC/foreach.m +++ b/clang/test/SemaObjC/foreach.m @@ -1,4 +1,4 @@ -/* RUN: %clang_cc1 -Wall -fsyntax-only -verify -std=c89 -pedantic %s +/* RUN: %clang_cc1 -Wall -Wno-unused-but-set-variable -fsyntax-only -verify -std=c89 -pedantic %s */ @class NSArray;