diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2019-11-19 16:32:49 +0100 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2019-11-22 12:57:32 +0100 |
commit | f853ec317f6af1b8c65cc5bd758371689c75118d (patch) | |
tree | b86d729bf9a9465ee619ead3b5635efa62a1804e /compilerplugins | |
parent | f31d36966bceb90e261cbecd42634bde4448d527 (diff) |
Extend loplugin:external to warn about classes
...following up on 314f15bff08b76bf96acf99141776ef64d2f1355 "Extend
loplugin:external to warn about enums".
Cases where free functions were moved into an unnamed namespace along with a
class, to not break ADL, are in:
filter/source/svg/svgexport.cxx
sc/source/filter/excel/xelink.cxx
sc/source/filter/excel/xilink.cxx
svx/source/sdr/contact/viewobjectcontactofunocontrol.cxx
All other free functions mentioning moved classes appear to be harmless and not
give rise to (silent, even) ADL breakage. (One remaining TODO in
compilerplugins/clang/external.cxx is that derived classes are not covered by
computeAffectedTypes, even though they could also be affected by ADL-breakage---
but don't seem to be in any acutal case across the code base.)
For friend declarations using elaborate type specifiers, like
class C1 {};
class C2 { friend class C1; };
* If C2 (but not C1) is moved into an unnamed namespace, the friend declaration
must be changed to not use an elaborate type specifier (i.e., "friend C1;"; see
C++17 [namespace.memdef]/3: "If the name in a friend declaration is neither
qualified nor a template-id and the declaration is a function or an
elaborated-type-specifier, the lookup to determine whether the entity has been
previously declared shall not consider any scopes outside the innermost
enclosing namespace.")
* If C1 (but not C2) is moved into an unnamed namespace, the friend declaration
must be changed too, see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71882>
"elaborated-type-specifier friend not looked up in unnamed namespace".
Apart from that, to keep changes simple and mostly mechanical (which should help
avoid regressions), out-of-line definitions of class members have been left in
the enclosing (named) namespace. But explicit specializations of class
templates had to be moved into the unnamed namespace to appease
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92598> "explicit specialization of
template from unnamed namespace using unqualified-id in enclosing namespace".
Also, accompanying declarations (of e.g. typedefs or static variables) that
could arguably be moved into the unnamed namespace too have been left alone.
And in some cases, mention of affected types in blacklists in other loplugins
needed to be adapted.
And sc/qa/unit/mark_test.cxx uses a hack of including other .cxx, one of which
is sc/source/core/data/segmenttree.cxx where e.g. ScFlatUInt16SegmentsImpl is
not moved into an unnamed namespace (because it is declared in
sc/inc/segmenttree.hxx), but its base ScFlatSegmentsImpl is. GCC warns about
such combinations with enabled-by-default -Wsubobject-linkage, but "The compiler
doesn’t give this warning for types defined in the main .C file, as those are
unlikely to have multiple definitions."
(<https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html>) The
warned-about classes also don't have multiple definitions in the given test, so
disable the warning when including the .cxx.
Change-Id: Ib694094c0d8168be68f8fe90dfd0acbb66a3f1e4
Reviewed-on: https://gerrit.libreoffice.org/83239
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/clang/badstatics.cxx | 2 | ||||
-rw-r--r-- | compilerplugins/clang/external.cxx | 10 | ||||
-rw-r--r-- | compilerplugins/clang/refcounting.cxx | 2 | ||||
-rw-r--r-- | compilerplugins/clang/staticmethods.cxx | 2 | ||||
-rw-r--r-- | compilerplugins/clang/test/external.cxx | 12 |
5 files changed, 20 insertions, 8 deletions
diff --git a/compilerplugins/clang/badstatics.cxx b/compilerplugins/clang/badstatics.cxx index 737330dd10c0..60abc11c222b 100644 --- a/compilerplugins/clang/badstatics.cxx +++ b/compilerplugins/clang/badstatics.cxx @@ -117,7 +117,7 @@ public: || type.Class("weak_ptr").StdNamespace() // not owning || type.Class("ImplWallpaper").GlobalNamespace() // very odd static instance here || type.Class("Application").GlobalNamespace() // numerous odd subclasses in vclmain::createApplication() - || type.Class("DemoMtfApp").GlobalNamespace() // one of these Application with own VclPtr + || type.Class("DemoMtfApp").AnonymousNamespace().GlobalNamespace() // one of these Application with own VclPtr ) { return std::make_pair(false, std::vector<FieldDecl const*>()); diff --git a/compilerplugins/clang/external.cxx b/compilerplugins/clang/external.cxx index 64da725cfff9..b31f620cf5ef 100644 --- a/compilerplugins/clang/external.cxx +++ b/compilerplugins/clang/external.cxx @@ -133,9 +133,6 @@ public: bool VisitTagDecl(TagDecl* decl) { - /*TODO:*/ - if (!isa<EnumDecl>(decl)) - return true; // in general, moving classes into an unnamed namespace can break ADL if (isa<ClassTemplateSpecializationDecl>(decl)) { return true; @@ -266,8 +263,6 @@ public: bool VisitClassTemplateDecl(ClassTemplateDecl* decl) { - /*TODO:*/ - return true; // in general, moving classes or enumerations into an unnamed namespace can break ADL if (!decl->isThisDeclarationADefinition()) { return true; @@ -329,6 +324,7 @@ private: } else { + //TODO: Derived types are also affected! CXXRecordDecl const* rec; if (auto const d = dyn_cast<ClassTemplateDecl>(decl)) { @@ -400,6 +396,10 @@ private: if (auto const d1 = dyn_cast<FriendDecl>(d)) { d = d1->getFriendDecl(); + if (d == nullptr) // happens for 'friend struct S;' + { + continue; + } } FunctionDecl const* f; if (auto const d1 = dyn_cast<FunctionTemplateDecl>(d)) diff --git a/compilerplugins/clang/refcounting.cxx b/compilerplugins/clang/refcounting.cxx index c002a1499977..168d775b28d2 100644 --- a/compilerplugins/clang/refcounting.cxx +++ b/compilerplugins/clang/refcounting.cxx @@ -256,7 +256,7 @@ bool containsSalhelperReferenceObjectSubclass(const clang::Type* pType0) { if (pTemplate) { auto const dc = loplugin::DeclCheck(pTemplate); if (dc.Class("Reference").Namespace("rtl").GlobalNamespace() - || (dc.Class("OStoreHandle").Namespace("store") + || (dc.Class("OStoreHandle").AnonymousNamespace().Namespace("store") .GlobalNamespace())) { return false; diff --git a/compilerplugins/clang/staticmethods.cxx b/compilerplugins/clang/staticmethods.cxx index 25e4d2c77473..6070ce860d82 100644 --- a/compilerplugins/clang/staticmethods.cxx +++ b/compilerplugins/clang/staticmethods.cxx @@ -173,7 +173,7 @@ bool StaticMethods::TraverseCXXMethodDecl(const CXXMethodDecl * pCXXMethodDecl) // used in a function-pointer-table if ((cdc.Class("SbiRuntime").GlobalNamespace() && startsWith(pCXXMethodDecl->getNameAsString(), "Step")) - || (cdc.Class("OoxFormulaParserImpl").Namespace("xls").Namespace("oox") + || (cdc.Class("OoxFormulaParserImpl").AnonymousNamespace().Namespace("xls").Namespace("oox") .GlobalNamespace()) || cdc.Class("SwTableFormula").GlobalNamespace() || (cdc.Class("BiffFormulaParserImpl").Namespace("xls").Namespace("oox") diff --git a/compilerplugins/clang/test/external.cxx b/compilerplugins/clang/test/external.cxx index 28b7c6df01b7..6eb486a57fc1 100644 --- a/compilerplugins/clang/test/external.cxx +++ b/compilerplugins/clang/test/external.cxx @@ -20,6 +20,7 @@ int const n2 = 0; // no warning, internal linkage constexpr int n3 = 0; // no warning, internal linkage +// expected-error@+1 {{externally available entity 'S1' is not previously declared in an included file (if it is only used in this translation unit, put it in an unnamed namespace; otherwise, provide a declaration of it in an included file) [loplugin:external]}} struct S1 { friend void f1() {} // no warning for injected function (no place where to mark it `static`) @@ -28,6 +29,7 @@ struct S1 friend void f2() {} }; +// expected-error@+1 {{externally available entity 'S2' is not previously declared in an included file (if it is only used in this translation unit, put it in an unnamed namespace; otherwise, provide a declaration of it in an included file) [loplugin:external]}} struct S2 { friend void f1(); @@ -76,11 +78,21 @@ extern "C++" { void fc(E const*); } +// expected-error@+1 {{externally available entity 'S1' is not previously declared in an included file (if it is only used in this translation unit, put it in an unnamed namespace; otherwise, provide a declaration of it in an included file) [loplugin:external]}} struct S1 { struct S2; // No note about associating function; injected friend function not found by ADL: friend void f2(E const*); + // expected-note@+1 {{a function associating 'N::S1' is declared here [loplugin:external]}} + friend void h(S1); +}; + +// expected-error@+1 {{externally available entity 'S3' is not previously declared in an included file (if it is only used in this translation unit, put it in an unnamed namespace; otherwise, provide a declaration of it in an included file) [loplugin:external]}} +struct S3 +{ + // expected-note@+1 {{another declaration is here [loplugin:external]}} + friend void h(S1); }; inline namespace I2 |