summaryrefslogtreecommitdiff
path: root/lib/Sema
diff options
context:
space:
mode:
authorNick Lewycky <nicholas@mxc.ca>2013-02-01 08:13:20 +0000
committerNick Lewycky <nicholas@mxc.ca>2013-02-01 08:13:20 +0000
commitcd0655b17249c4c4908ca91462657f62285017e6 (patch)
treef591e7ddc3ef7fdc95b065a9ae56a34d7b42c906 /lib/Sema
parentbe507b6e72df8ab5e7d8c31eb4453e1bdf5fcfaf (diff)
downloadclang-cd0655b17249c4c4908ca91462657f62285017e6.tar.gz
Add a new -Wundefined-inline warning for inline functions which are used but not
defined. Fixes PR14993! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@174158 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema')
-rw-r--r--lib/Sema/MultiplexExternalSemaSource.cpp4
-rw-r--r--lib/Sema/Sema.cpp54
-rw-r--r--lib/Sema/SemaDecl.cpp32
-rw-r--r--lib/Sema/SemaExpr.cpp12
4 files changed, 71 insertions, 31 deletions
diff --git a/lib/Sema/MultiplexExternalSemaSource.cpp b/lib/Sema/MultiplexExternalSemaSource.cpp
index 609324d32d..ecce6d8e7b 100644
--- a/lib/Sema/MultiplexExternalSemaSource.cpp
+++ b/lib/Sema/MultiplexExternalSemaSource.cpp
@@ -201,10 +201,10 @@ void MultiplexExternalSemaSource::ReadKnownNamespaces(
Sources[i]->ReadKnownNamespaces(Namespaces);
}
-void MultiplexExternalSemaSource::ReadUndefinedInternals(
+void MultiplexExternalSemaSource::ReadUndefinedButUsed(
llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined){
for(size_t i = 0; i < Sources.size(); ++i)
- Sources[i]->ReadUndefinedInternals(Undefined);
+ Sources[i]->ReadUndefinedButUsed(Undefined);
}
bool MultiplexExternalSemaSource::LookupUnqualified(LookupResult &R, Scope *S){
diff --git a/lib/Sema/Sema.cpp b/lib/Sema/Sema.cpp
index 76ea996ec5..5e5011b7f6 100644
--- a/lib/Sema/Sema.cpp
+++ b/lib/Sema/Sema.cpp
@@ -366,12 +366,16 @@ static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) {
}
namespace {
- struct SortUndefinedInternal {
+ struct SortUndefinedButUsed {
const SourceManager &SM;
- explicit SortUndefinedInternal(SourceManager &SM) : SM(SM) {}
+ explicit SortUndefinedButUsed(SourceManager &SM) : SM(SM) {}
bool operator()(const std::pair<NamedDecl *, SourceLocation> &l,
const std::pair<NamedDecl *, SourceLocation> &r) const {
+ if (l.second.isValid() && !r.second.isValid())
+ return true;
+ if (!l.second.isValid() && r.second.isValid())
+ return false;
if (l.second != r.second)
return SM.isBeforeInTranslationUnit(l.second, r.second);
return SM.isBeforeInTranslationUnit(l.first->getLocation(),
@@ -381,55 +385,65 @@ namespace {
}
/// Obtains a sorted list of functions that are undefined but ODR-used.
-void Sema::getUndefinedInternals(
+void Sema::getUndefinedButUsed(
SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> > &Undefined) {
for (llvm::DenseMap<NamedDecl *, SourceLocation>::iterator
- I = UndefinedInternals.begin(), E = UndefinedInternals.end();
+ I = UndefinedButUsed.begin(), E = UndefinedButUsed.end();
I != E; ++I) {
NamedDecl *ND = I->first;
// Ignore attributes that have become invalid.
if (ND->isInvalidDecl()) continue;
- // If we found out that the decl is external, don't warn.
- if (ND->getLinkage() == ExternalLinkage) continue;
-
// __attribute__((weakref)) is basically a definition.
if (ND->hasAttr<WeakRefAttr>()) continue;
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
if (FD->isDefined())
continue;
+ if (FD->getLinkage() == ExternalLinkage &&
+ !FD->getMostRecentDecl()->isInlined())
+ continue;
} else {
if (cast<VarDecl>(ND)->hasDefinition() != VarDecl::DeclarationOnly)
continue;
+ if (ND->getLinkage() == ExternalLinkage)
+ continue;
}
Undefined.push_back(std::make_pair(ND, I->second));
}
- // Sort (in order of use site) so that we're not (as) dependent on
- // the iteration order through an llvm::DenseMap.
+ // Sort (in order of use site) so that we're not dependent on the iteration
+ // order through an llvm::DenseMap.
std::sort(Undefined.begin(), Undefined.end(),
- SortUndefinedInternal(Context.getSourceManager()));
+ SortUndefinedButUsed(Context.getSourceManager()));
}
-/// checkUndefinedInternals - Check for undefined objects with internal linkage.
-static void checkUndefinedInternals(Sema &S) {
- if (S.UndefinedInternals.empty()) return;
+/// checkUndefinedButUsed - Check for undefined objects with internal linkage
+/// or that are inline.
+static void checkUndefinedButUsed(Sema &S) {
+ if (S.UndefinedButUsed.empty()) return;
// Collect all the still-undefined entities with internal linkage.
SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
- S.getUndefinedInternals(Undefined);
+ S.getUndefinedButUsed(Undefined);
if (Undefined.empty()) return;
for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
NamedDecl *ND = I->first;
- S.Diag(ND->getLocation(), diag::warn_undefined_internal)
- << isa<VarDecl>(ND) << ND;
- S.Diag(I->second, diag::note_used_here);
+ if (ND->getLinkage() != ExternalLinkage) {
+ S.Diag(ND->getLocation(), diag::warn_undefined_internal)
+ << isa<VarDecl>(ND) << ND;
+ } else {
+ assert(cast<FunctionDecl>(ND)->getMostRecentDecl()->isInlined() &&
+ "used object requires definition but isn't inline or internal?");
+ S.Diag(ND->getLocation(), diag::warn_undefined_inline) << ND;
+ }
+ if (I->second.isValid())
+ S.Diag(I->second, diag::note_used_here);
}
}
@@ -743,8 +757,8 @@ void Sema::ActOnEndOfTranslationUnit() {
}
if (ExternalSource)
- ExternalSource->ReadUndefinedInternals(UndefinedInternals);
- checkUndefinedInternals(*this);
+ ExternalSource->ReadUndefinedButUsed(UndefinedButUsed);
+ checkUndefinedButUsed(*this);
}
if (Diags.getDiagnosticLevel(diag::warn_unused_private_field,
@@ -1088,7 +1102,7 @@ void ExternalSemaSource::ReadKnownNamespaces(
SmallVectorImpl<NamespaceDecl *> &Namespaces) {
}
-void ExternalSemaSource::ReadUndefinedInternals(
+void ExternalSemaSource::ReadUndefinedButUsed(
llvm::DenseMap<NamedDecl *, SourceLocation> &Undefined) {
}
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index 4404c6f9a0..0d549fcc18 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -2215,6 +2215,23 @@ bool Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD, Scope *S) {
New->setType(QualType(NewType, 0));
NewQType = Context.getCanonicalType(New->getType());
}
+
+ // If this redeclaration makes the function inline, we may need to add it to
+ // UndefinedButUsed.
+ if (!Old->isInlined() && New->isInlined() &&
+ !New->hasAttr<GNUInlineAttr>() &&
+ (getLangOpts().CPlusPlus || !getLangOpts().GNUInline) &&
+ Old->isUsed(false) &&
+ !Old->isDefined() && !New->isThisDeclarationADefinition())
+ UndefinedButUsed.insert(std::make_pair(Old->getCanonicalDecl(),
+ SourceLocation()));
+
+ // If this redeclaration makes it newly gnu_inline, we don't want to warn
+ // about it.
+ if (New->hasAttr<GNUInlineAttr>() &&
+ Old->isInlined() && !Old->hasAttr<GNUInlineAttr>()) {
+ UndefinedButUsed.erase(Old->getCanonicalDecl());
+ }
if (getLangOpts().CPlusPlus) {
// (C++98 13.1p2):
@@ -8431,12 +8448,17 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body,
if (FD) {
FD->setBody(Body);
- // The only way to be included in UndefinedInternals is if there is an
- // ODR-use before the definition. Avoid the expensive map lookup if this
+ // The only way to be included in UndefinedButUsed is if there is an
+ // ODR use before the definition. Avoid the expensive map lookup if this
// is the first declaration.
- if (FD->getPreviousDecl() != 0 && FD->getPreviousDecl()->isUsed() &&
- FD->getLinkage() != ExternalLinkage)
- UndefinedInternals.erase(FD);
+ if (FD->getPreviousDecl() != 0 && FD->getPreviousDecl()->isUsed()) {
+ if (FD->getLinkage() != ExternalLinkage)
+ UndefinedButUsed.erase(FD);
+ else if (FD->isInlined() &&
+ (LangOpts.CPlusPlus || !LangOpts.GNUInline) &&
+ (!FD->getPreviousDecl()->hasAttr<GNUInlineAttr>()))
+ UndefinedButUsed.erase(FD);
+ }
// If the function implicitly returns zero (like 'main') or is naked,
// don't complain about missing return statements.
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 87d75f630f..5dd75ced97 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -10506,9 +10506,13 @@ void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func) {
}
// Keep track of used but undefined functions.
- if (!Func->isDefined() && Func->getLinkage() != ExternalLinkage) {
- SourceLocation &old = UndefinedInternals[Func->getCanonicalDecl()];
- if (old.isInvalid()) old = Loc;
+ if (!Func->isDefined()) {
+ if (Func->getLinkage() != ExternalLinkage)
+ UndefinedButUsed.insert(std::make_pair(Func->getCanonicalDecl(), Loc));
+ else if (Func->getMostRecentDecl()->isInlined() &&
+ (LangOpts.CPlusPlus || !LangOpts.GNUInline) &&
+ !Func->getMostRecentDecl()->hasAttr<GNUInlineAttr>())
+ UndefinedButUsed.insert(std::make_pair(Func->getCanonicalDecl(), Loc));
}
// Normally the must current decl is marked used while processing the use and
@@ -11021,7 +11025,7 @@ static void MarkVarDeclODRUsed(Sema &SemaRef, VarDecl *Var,
if (Var->hasDefinition(SemaRef.Context) == VarDecl::DeclarationOnly &&
Var->getLinkage() != ExternalLinkage &&
!(Var->isStaticDataMember() && Var->hasInit())) {
- SourceLocation &old = SemaRef.UndefinedInternals[Var->getCanonicalDecl()];
+ SourceLocation &old = SemaRef.UndefinedButUsed[Var->getCanonicalDecl()];
if (old.isInvalid()) old = Loc;
}