aboutsummaryrefslogtreecommitdiff
path: root/lib/Sema/SemaExpr.cpp
diff options
context:
space:
mode:
authorJordan Rose <jordan_rose@apple.com>2012-06-20 18:50:06 +0000
committerJordan Rose <jordan_rose@apple.com>2012-06-20 18:50:06 +0000
commit33c0f3703de3ab6d8f3a41d7fb9d2b3489617b21 (patch)
tree81268e1b51772add37ef26eb5fc2cb6155b128d3 /lib/Sema/SemaExpr.cpp
parent52e4ed949ba52958db1e938860e3b49944ce8680 (diff)
Remove -Winternal-linkage-in-inline in C++.
It's very easy for anonymous external linkage to propagate in C++ through return types and parameter types. Likewise, it's possible that a template containing an inline function is only used with parameters that have internal linkage. Actually diagnosing where the internal linkage comes from is fairly difficult (both to locate and then to print nicely). Finally, since we only have one translation unit available, we can't even prove that any of this violates the ODR. This warning needs better-defined behavior in C++ before it can really go in. Rewording of the C warning (which /is/ specified by C99) coming shortly. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@158836 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/SemaExpr.cpp')
-rw-r--r--lib/Sema/SemaExpr.cpp74
1 files changed, 16 insertions, 58 deletions
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 47fac59a34..57f1605fbe 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -143,26 +143,21 @@ static bool hasAnyExplicitStorageClass(const FunctionDecl *D) {
}
/// \brief Check whether we're in an extern inline function and referring to a
-/// variable or function with internal linkage.
+/// variable or function with internal linkage (C11 6.7.4p3).
///
-/// This also applies to anonymous-namespaced objects, which are effectively
-/// internal.
/// This is only a warning because we used to silently accept this code, but
-/// most likely it will not do what the user intends.
+/// in many cases it will not behave correctly. This is not enabled in C++ mode
+/// because the restriction language is a bit weaker (C++11 [basic.def.odr]p6)
+/// and so while there may still be user mistakes, most of the time we can't
+/// prove that there are errors.
static void diagnoseUseOfInternalDeclInInlineFunction(Sema &S,
const NamedDecl *D,
SourceLocation Loc) {
- // C11 6.7.4p3: An inline definition of a function with external linkage...
- // shall not contain a reference to an identifier with internal linkage.
- // C++11 [basic.def.odr]p6: ...in each definition of D, corresponding names,
- // looked up according to 3.4, shall refer to an entity defined within the
- // definition of D, or shall refer to the same entity, after overload
- // resolution (13.3) and after matching of partial template specialization
- // (14.8.3), except that a name can refer to a const object with internal or
- // no linkage if the object has the same literal type in all definitions of
- // D, and the object is initialized with a constant expression (5.19), and
- // the value (but not the address) of the object is used, and the object has
- // the same value in all definitions of D; ...
+ // This is disabled under C++; there are too many ways for this to fire in
+ // contexts where the warning is a false positive, or where it is technically
+ // correct but benign.
+ if (S.getLangOpts().CPlusPlus)
+ return;
// Check if this is an inlined function or method.
FunctionDecl *Current = S.getCurFunctionDecl();
@@ -174,56 +169,19 @@ static void diagnoseUseOfInternalDeclInInlineFunction(Sema &S,
return;
// Check if the decl has internal linkage.
- Linkage UsedLinkage = D->getLinkage();
- switch (UsedLinkage) {
- case NoLinkage:
- return;
- case InternalLinkage:
- case UniqueExternalLinkage:
- break;
- case ExternalLinkage:
+ if (D->getLinkage() != InternalLinkage)
return;
- }
-
- // Check C++'s exception for const variables. This is in the standard
- // because in C++ const variables have internal linkage unless
- // explicitly declared extern.
- // Note that we don't do any of the cross-TU checks, and this code isn't
- // even particularly careful about checking if the variable "has the
- // same value in all definitions" of the inline function. It just does a
- // sanity check to make sure there is an initializer at all.
- // FIXME: We should still be checking to see if we're using a constant
- // as a glvalue anywhere, but we don't have the necessary information to
- // do that here, and as long as this is a warning and not a hard error
- // it's not appropriate to change the semantics of the program (i.e.
- // by having BuildDeclRefExpr use VK_RValue for constants like these).
- const VarDecl *VD = dyn_cast<VarDecl>(D);
- if (VD && S.getLangOpts().CPlusPlus)
- if (VD->getType().isConstant(S.getASTContext()) && VD->getAnyInitializer())
- return;
// Don't warn unless -pedantic is on if the inline function is in the main
- // source file, and in C++ don't warn at all, since the one-definition rule is
- // still satisfied. This function will most likely not be inlined into
+ // source file. This function will most likely not be inlined into
// another translation unit, so it's effectively internal.
bool IsInMainFile = S.getSourceManager().isFromMainFile(Loc);
- if (S.getLangOpts().CPlusPlus) {
- if (IsInMainFile)
- return;
-
- S.Diag(Loc, diag::warn_internal_in_extern_inline_cxx)
- << (bool)VD << D
- << (UsedLinkage == UniqueExternalLinkage)
- << isa<CXXMethodDecl>(Current);
- } else {
- S.Diag(Loc, IsInMainFile ? diag::ext_internal_in_extern_inline
- : diag::warn_internal_in_extern_inline)
- << (bool)VD << D;
- }
+ S.Diag(Loc, IsInMainFile ? diag::ext_internal_in_extern_inline
+ : diag::warn_internal_in_extern_inline)
+ << isa<VarDecl>(D) << D;
// Suggest "static" on the inline function, if possible.
- if (!isa<CXXMethodDecl>(Current) &&
- !hasAnyExplicitStorageClass(Current)) {
+ if (!hasAnyExplicitStorageClass(Current)) {
const FunctionDecl *FirstDecl = Current->getCanonicalDecl();
SourceLocation DeclBegin = FirstDecl->getSourceRange().getBegin();
S.Diag(DeclBegin, diag::note_convert_inline_to_static)