зеркало из https://github.com/microsoft/clang-1.git
The unused warnings extravaganza continues. Warn for:
-static variables -variables in anonymous namespace (fixes rdar://7794535) -static data members in anonymous namespace -static data members specializations in anonymous namespace git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@111027 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Родитель
f6d1d43d68
Коммит
30c0dd86f0
|
@ -349,8 +349,7 @@ public:
|
|||
std::vector<VarDecl *> TentativeDefinitions;
|
||||
|
||||
/// \brief The set of file scoped decls seen so far that have not been used
|
||||
/// and must warn if not used. For functions only contains the first
|
||||
/// declaration.
|
||||
/// and must warn if not used. Only contains the first declaration.
|
||||
std::vector<const DeclaratorDecl*> UnusedFileScopedDecls;
|
||||
|
||||
class AccessedEntity {
|
||||
|
|
|
@ -241,15 +241,33 @@ static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) {
|
|||
return true;
|
||||
|
||||
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
||||
// For functions, UnusedFileScopedDecls stores the first declaration.
|
||||
// UnusedFileScopedDecls stores the first declaration.
|
||||
// The declaration may have become definition so check again.
|
||||
const FunctionDecl *DeclToCheck;
|
||||
if (FD->hasBody(DeclToCheck))
|
||||
return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
|
||||
|
||||
// Later redecls may add new information resulting in not having to warn,
|
||||
// so check again.
|
||||
const FunctionDecl *DeclToCheck;
|
||||
if (!FD->hasBody(DeclToCheck))
|
||||
DeclToCheck = FD->getMostRecentDeclaration();
|
||||
DeclToCheck = FD->getMostRecentDeclaration();
|
||||
if (DeclToCheck != FD)
|
||||
return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
|
||||
}
|
||||
|
||||
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
||||
// UnusedFileScopedDecls stores the first declaration.
|
||||
// The declaration may have become definition so check again.
|
||||
const VarDecl *DeclToCheck = VD->getDefinition();
|
||||
if (DeclToCheck)
|
||||
return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
|
||||
|
||||
// Later redecls may add new information resulting in not having to warn,
|
||||
// so check again.
|
||||
DeclToCheck = VD->getMostRecentDeclaration();
|
||||
if (DeclToCheck != VD)
|
||||
return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -358,9 +376,13 @@ void Sema::ActOnEndOfTranslationUnit() {
|
|||
DiagD = FD;
|
||||
Diag(DiagD->getLocation(), diag::warn_unused_function)
|
||||
<< DiagD->getDeclName();
|
||||
} else
|
||||
Diag((*I)->getLocation(), diag::warn_unused_variable)
|
||||
<< cast<VarDecl>(*I)->getDeclName();
|
||||
} else {
|
||||
const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition();
|
||||
if (!DiagD)
|
||||
DiagD = cast<VarDecl>(*I);
|
||||
Diag(DiagD->getLocation(), diag::warn_unused_variable)
|
||||
<< DiagD->getDeclName();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -523,15 +523,17 @@ static void RemoveUsingDecls(LookupResult &R) {
|
|||
|
||||
bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const {
|
||||
assert(D);
|
||||
|
||||
if (D->isInvalidDecl() || D->isUsed() || D->hasAttr<UnusedAttr>())
|
||||
return false;
|
||||
if (D->getLinkage() == ExternalLinkage)
|
||||
return false;
|
||||
|
||||
// Ignore class templates.
|
||||
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D))
|
||||
if (MD->getParent()->getDescribedClassTemplate())
|
||||
return false;
|
||||
if (D->getDeclContext()->isDependentContext())
|
||||
return false;
|
||||
|
||||
// We warn for unused decls internal to the translation unit.
|
||||
if (D->getLinkage() == ExternalLinkage)
|
||||
return false;
|
||||
|
||||
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
||||
if (FD->isThisDeclarationADefinition())
|
||||
|
@ -539,6 +541,10 @@ bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const {
|
|||
return true;
|
||||
}
|
||||
|
||||
if (const VarDecl *VD = dyn_cast<VarDecl>(D))
|
||||
if (VD->isFileVarDecl())
|
||||
return !Context.DeclMustBeEmitted(VD);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -552,6 +558,12 @@ bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const {
|
|||
return; // First should already be in the vector.
|
||||
}
|
||||
|
||||
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
||||
const VarDecl *First = VD->getFirstDeclaration();
|
||||
if (VD != First && ShouldWarnIfUnusedFileScopedDecl(First))
|
||||
return; // First should already be in the vector.
|
||||
}
|
||||
|
||||
if (ShouldWarnIfUnusedFileScopedDecl(D))
|
||||
UnusedFileScopedDecls.push_back(D);
|
||||
}
|
||||
|
@ -2758,6 +2770,8 @@ Sema::ActOnVariableDeclarator(Scope* S, Declarator& D, DeclContext* DC,
|
|||
if (NewVD->getLinkage() == ExternalLinkage && !DC->isRecord())
|
||||
AddPushedVisibilityAttribute(NewVD);
|
||||
|
||||
MarkUnusedFileScopedDecl(NewVD);
|
||||
|
||||
return NewVD;
|
||||
}
|
||||
|
||||
|
|
|
@ -449,7 +449,9 @@ Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
|
|||
// Diagnose unused local variables.
|
||||
if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
|
||||
SemaRef.DiagnoseUnusedDecl(Var);
|
||||
|
||||
|
||||
SemaRef.MarkUnusedFileScopedDecl(Var);
|
||||
|
||||
return Var;
|
||||
}
|
||||
|
||||
|
|
|
@ -25,3 +25,19 @@ namespace {
|
|||
}
|
||||
|
||||
void S::m3() { } // expected-warning{{unused}}
|
||||
|
||||
static int x1; // expected-warning{{unused}}
|
||||
|
||||
namespace {
|
||||
int x2; // expected-warning{{unused}}
|
||||
|
||||
struct S2 {
|
||||
static int x; // expected-warning{{unused}}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct TS2 {
|
||||
static int x;
|
||||
};
|
||||
template <> int TS2<int>::x; // expected-warning{{unused}}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче