summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2020-02-13 12:12:59 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2020-02-13 19:30:06 +0100
commitabc0344a234567aee0edcb4523036758d966481d (patch)
tree4886834071b3ceab9087d12aa208afd200a82777 /compilerplugins
parenta39a6476fdbda5adf963b836b6740665affa39d5 (diff)
convert conststringvar plugin to shared infrastructre
and move the duplicated hasExternalLinkage function to a common location Change-Id: I39a1990945666ff7a307b4ddd5e270da64ee1673 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/88592 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/conststringvar.cxx53
-rw-r--r--compilerplugins/clang/externvar.cxx27
-rw-r--r--compilerplugins/clang/plugin.cxx25
-rw-r--r--compilerplugins/clang/plugin.hxx5
4 files changed, 54 insertions, 56 deletions
diff --git a/compilerplugins/clang/conststringvar.cxx b/compilerplugins/clang/conststringvar.cxx
index a323b1cf0480..c5e83722ac47 100644
--- a/compilerplugins/clang/conststringvar.cxx
+++ b/compilerplugins/clang/conststringvar.cxx
@@ -6,6 +6,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
+#ifndef LO_CLANG_SHARED_PLUGINS
#include <set>
#include <stack>
@@ -19,31 +20,6 @@
namespace {
-// It looks like Clang wrongly implements DR 4
-// (<http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#4>) and treats
-// a variable declared in an 'extern "..." {...}'-style linkage-specification as
-// if it contained the 'extern' specifier:
-bool hasExternalLinkage(VarDecl const * decl) {
- if (decl->getLinkageAndVisibility().getLinkage() != ExternalLinkage) {
- return false;
- }
- for (auto ctx = decl->getLexicalDeclContext();
- ctx->getDeclKind() != Decl::TranslationUnit;
- ctx = ctx->getLexicalParent())
- {
- if (auto ls = dyn_cast<LinkageSpecDecl>(ctx)) {
- if (!ls->hasBraces()) {
- return true;
- }
- if (auto prev = decl->getPreviousDecl()) {
- return hasExternalLinkage(prev);
- }
- return !decl->isInAnonymousNamespace();
- }
- }
- return true;
-}
-
class ConstStringVar:
public loplugin::FilteringPlugin<ConstStringVar>
{
@@ -65,7 +41,7 @@ public:
}
}
- bool TraverseImplicitCastExpr(ImplicitCastExpr * expr) {
+ bool PreTraverseImplicitCastExpr(ImplicitCastExpr * expr) {
bool match;
switch (expr->getCastKind()) {
case CK_NoOp:
@@ -94,11 +70,25 @@ public:
}
}
}
- bool b = RecursiveASTVisitor::TraverseImplicitCastExpr(expr);
+ pushed_.push(pushed);
+ return true;
+ }
+ bool PostTraverseImplicitCastExpr(ImplicitCastExpr *, bool) {
+ bool pushed = pushed_.top();
+ pushed_.pop();
if (pushed) {
casted_.pop();
}
- return b;
+ return true;
+ }
+ bool TraverseImplicitCastExpr(ImplicitCastExpr * expr) {
+ bool ret = true;
+ if (PreTraverseImplicitCastExpr(expr))
+ {
+ ret = FilteringPlugin::TraverseImplicitCastExpr(expr);
+ PostTraverseImplicitCastExpr(expr, ret);
+ }
+ return ret;
}
bool VisitVarDecl(VarDecl const * decl) {
@@ -108,7 +98,7 @@ public:
if (decl != decl->getCanonicalDecl()) {
return true;
}
- if (isa<ParmVarDecl>(decl) || hasExternalLinkage(decl)) {
+ if (isa<ParmVarDecl>(decl) || loplugin::hasExternalLinkage(decl)) {
return true;
}
if (!loplugin::TypeCheck(decl->getType()).NonConstVolatile().Pointer()
@@ -147,10 +137,13 @@ public:
private:
std::set<VarDecl const *> vars_;
std::stack<DeclRefExpr const *> casted_;
+ std::stack<bool> pushed_;
};
-loplugin::Plugin::Registration<ConstStringVar> X("conststringvar");
+loplugin::Plugin::Registration<ConstStringVar> conststringvar("conststringvar");
}
+#endif // LO_CLANG_SHARED_PLUGINS
+
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/compilerplugins/clang/externvar.cxx b/compilerplugins/clang/externvar.cxx
index eb20d3f36b0d..05e9820d4da3 100644
--- a/compilerplugins/clang/externvar.cxx
+++ b/compilerplugins/clang/externvar.cxx
@@ -17,31 +17,6 @@
namespace {
-// It looks like Clang wrongly implements DR 4
-// (<http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#4>) and treats
-// a variable declared in an 'extern "..." {...}'-style linkage-specification as
-// if it contained the 'extern' specifier:
-bool hasExternalLinkage(VarDecl const * decl) {
- if (decl->getLinkageAndVisibility().getLinkage() != ExternalLinkage) {
- return false;
- }
- for (auto ctx = decl->getLexicalDeclContext();
- ctx->getDeclKind() != Decl::TranslationUnit;
- ctx = ctx->getLexicalParent())
- {
- if (auto ls = dyn_cast<LinkageSpecDecl>(ctx)) {
- if (!ls->hasBraces()) {
- return true;
- }
- if (auto prev = decl->getPreviousDecl()) {
- return hasExternalLinkage(prev);
- }
- return !decl->isInAnonymousNamespace();
- }
- }
- return true;
-}
-
class ExternVar: public loplugin::FilteringPlugin<ExternVar>
{
public:
@@ -60,7 +35,7 @@ public:
}
if (!(decl->isFirstDecl()
&& compiler.getSourceManager().isInMainFile(decl->getLocation())
- && hasExternalLinkage(decl)))
+ && loplugin::hasExternalLinkage(decl)))
{
return true;
}
diff --git a/compilerplugins/clang/plugin.cxx b/compilerplugins/clang/plugin.cxx
index 6b89f3a89494..41767369211a 100644
--- a/compilerplugins/clang/plugin.cxx
+++ b/compilerplugins/clang/plugin.cxx
@@ -768,6 +768,31 @@ int derivedFromCount(QualType subclassQt, QualType baseclassQt)
return derivedFromCount(subclassRecordDecl, baseclassRecordDecl);
}
+// It looks like Clang wrongly implements DR 4
+// (<http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#4>) and treats
+// a variable declared in an 'extern "..." {...}'-style linkage-specification as
+// if it contained the 'extern' specifier:
+bool hasExternalLinkage(VarDecl const * decl) {
+ if (decl->getLinkageAndVisibility().getLinkage() != ExternalLinkage) {
+ return false;
+ }
+ for (auto ctx = decl->getLexicalDeclContext();
+ ctx->getDeclKind() != Decl::TranslationUnit;
+ ctx = ctx->getLexicalParent())
+ {
+ if (auto ls = dyn_cast<LinkageSpecDecl>(ctx)) {
+ if (!ls->hasBraces()) {
+ return true;
+ }
+ if (auto prev = decl->getPreviousDecl()) {
+ return hasExternalLinkage(prev);
+ }
+ return !decl->isInAnonymousNamespace();
+ }
+ }
+ return true;
+}
+
} // namespace
diff --git a/compilerplugins/clang/plugin.hxx b/compilerplugins/clang/plugin.hxx
index d6df6b522e31..829d00518f50 100644
--- a/compilerplugins/clang/plugin.hxx
+++ b/compilerplugins/clang/plugin.hxx
@@ -300,6 +300,11 @@ bool hasCLanguageLinkageType(FunctionDecl const * decl);
int derivedFromCount(clang::QualType subclassType, clang::QualType baseclassType);
int derivedFromCount(const CXXRecordDecl* subtypeRecord, const CXXRecordDecl* baseRecord);
+// It looks like Clang wrongly implements DR 4
+// (<http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#4>) and treats
+// a variable declared in an 'extern "..." {...}'-style linkage-specification as
+// if it contained the 'extern' specifier:
+bool hasExternalLinkage(VarDecl const * decl);
} // namespace