[Libreoffice-commits] core.git: Branch 'libreoffice-5-2' - compilerplugins/clang

Stephan Bergmann sbergman at redhat.com
Thu Jun 30 08:19:52 UTC 2016


 compilerplugins/clang/staticmethods.cxx |    9 +++--
 compilerplugins/clang/typecheck.cxx     |    6 +++
 compilerplugins/clang/typecheck.hxx     |   57 +++++++++++++++++++++++++++-----
 3 files changed, 61 insertions(+), 11 deletions(-)

New commits:
commit be9cd27b83c573bb143bd2778d6c63828e59aa3e
Author: Stephan Bergmann <sbergman at redhat.com>
Date:   Tue Jun 28 17:48:22 2016 +0200

    More adaption to Clang 3.4
    
    ...where NmaedDecl::getQualifiedNameAsString (which is expensive and bad,
    anyway) apparently returns something other than "(anonymous namespace)"
    
    Change-Id: I05ef96665c48f8f596dd0d317388e91a75b8307b
    (cherry picked from commit c1ab6613ae7b45f2d90aafd6c6a829a471ceca55)
    Reviewed-on: https://gerrit.libreoffice.org/26778
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Caolán McNamara <caolanm at redhat.com>
    Tested-by: Caolán McNamara <caolanm at redhat.com>

diff --git a/compilerplugins/clang/staticmethods.cxx b/compilerplugins/clang/staticmethods.cxx
index be014fd..9108e02 100644
--- a/compilerplugins/clang/staticmethods.cxx
+++ b/compilerplugins/clang/staticmethods.cxx
@@ -9,8 +9,9 @@
 
 #include "clang/AST/Attr.h"
 
-#include "plugin.hxx"
 #include "compat.hxx"
+#include "plugin.hxx"
+#include "typecheck.hxx"
 
 /*
   Look for member functions that can be static
@@ -118,12 +119,14 @@ bool StaticMethods::TraverseCXXMethodDecl(const CXXMethodDecl * pCXXMethodDecl)
     }
     // can't change it because in debug mode it can't be static
     // sal/cpprt/operators_new_delete.cxx
-    if (aParentName == "(anonymous namespace)::AllocatorTraits") {
+    auto dc = loplugin::DeclCheck(pCXXMethodDecl->getParent());
+    if (dc.Struct("AllocatorTraits").AnonymousNamespace().GlobalNamespace()) {
         return true;
     }
     // in this case, the code is taking the address of the member function
     // shell/source/unix/sysshell/recently_used_file_handler.cxx
-    if (aParentName == "(anonymous namespace)::recently_used_item") {
+    if (dc.Struct("recently_used_item").AnonymousNamespace().GlobalNamespace())
+    {
         return true;
     }
     // the unotools and svl config code stuff is doing weird stuff with a reference-counted statically allocated pImpl class
diff --git a/compilerplugins/clang/typecheck.cxx b/compilerplugins/clang/typecheck.cxx
index 490deb8..dcfc6db 100644
--- a/compilerplugins/clang/typecheck.cxx
+++ b/compilerplugins/clang/typecheck.cxx
@@ -59,6 +59,12 @@ TerminalCheck NamespaceCheck::StdNamespace() const {
         context_ != nullptr && compat::isStdNamespace(*context_));
 }
 
+NamespaceCheck NamespaceCheck::AnonymousNamespace() const {
+    auto n = llvm::dyn_cast_or_null<clang::NamespaceDecl>(context_);
+    return NamespaceCheck(
+        n != nullptr && n->isAnonymousNamespace() ? n->getParent() : nullptr);
+}
+
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/compilerplugins/clang/typecheck.hxx b/compilerplugins/clang/typecheck.hxx
index 70e3d8c..02c6382 100644
--- a/compilerplugins/clang/typecheck.hxx
+++ b/compilerplugins/clang/typecheck.hxx
@@ -21,6 +21,13 @@ namespace loplugin {
 class NamespaceCheck;
 class TerminalCheck;
 
+namespace detail {
+
+template<std::size_t N> NamespaceCheck checkRecordDecl(
+    clang::Decl const * decl, clang::TagTypeKind tag, char const (& id)[N]);
+
+}
+
 class TypeCheck {
 public:
     explicit TypeCheck(clang::QualType type): type_(type) {}
@@ -55,8 +62,12 @@ public:
 
     TerminalCheck StdNamespace() const;
 
+    NamespaceCheck AnonymousNamespace() const;
+
 private:
-    friend class TypeCheck;
+    friend TypeCheck;
+    template<std::size_t N> friend NamespaceCheck detail::checkRecordDecl(
+        clang::Decl const * decl, clang::TagTypeKind tag, char const (& id)[N]);
 
     explicit NamespaceCheck(clang::DeclContext const * context = nullptr):
         context_(context) {}
@@ -64,6 +75,19 @@ private:
     clang::DeclContext const * const context_;
 };
 
+class DeclCheck {
+public:
+    explicit DeclCheck(clang::Decl const * decl): decl_(decl) {}
+
+    explicit operator bool() const { return decl_ != nullptr; }
+
+    template<std::size_t N> inline NamespaceCheck Struct(char const (& id)[N])
+        const;
+
+private:
+    clang::Decl const * const decl_;
+};
+
 class TerminalCheck {
 public:
     explicit operator bool() const { return satisfied_; }
@@ -77,19 +101,30 @@ private:
     bool const satisfied_;
 };
 
+namespace detail {
+
+template<std::size_t N> NamespaceCheck checkRecordDecl(
+    clang::Decl const * decl, clang::TagTypeKind tag, char const (& id)[N])
+{
+    auto r = llvm::dyn_cast_or_null<clang::RecordDecl>(decl);
+    if (r != nullptr && r->getTagKind() == tag) {
+        auto const i = r->getIdentifier();
+        if (i != nullptr && i->isStr(id)) {
+            return NamespaceCheck(r->getDeclContext());
+        }
+    }
+    return NamespaceCheck();
+}
+
+}
+
 template<std::size_t N> NamespaceCheck TypeCheck::Class(char const (& id)[N])
     const
 {
     if (!type_.isNull()) {
         auto const t = type_->getAs<clang::RecordType>();
         if (t != nullptr) {
-            auto const d = t->getDecl();
-            if (d->isClass()) {
-                auto const i = d->getIdentifier();
-                if (i != nullptr && i->isStr(id)) {
-                    return NamespaceCheck(d->getDeclContext());
-                }
-            }
+            return detail::checkRecordDecl(t->getDecl(), clang::TTK_Class, id);
         }
     }
     return NamespaceCheck();
@@ -110,6 +145,12 @@ template<std::size_t N> NamespaceCheck NamespaceCheck::Namespace(
     return NamespaceCheck();
 }
 
+template<std::size_t N> NamespaceCheck DeclCheck::Struct(char const (& id)[N])
+    const
+{
+    return detail::checkRecordDecl(decl_, clang::TTK_Struct, id);
+}
+
 }
 
 #endif


More information about the Libreoffice-commits mailing list