aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergio Martins <smartins@kde.org>2021-01-10 00:13:30 +0000
committerSergio Martins <smartins@kde.org>2021-01-10 00:14:47 +0000
commit0215389de14f5bc6daa5d6e6e5d3943c0aba9584 (patch)
tree272c7ecf23c2016afbe1d7fbc5ce01e292bb2b3c
parent4feafb957c40267b96aeba84a35a87dfc613f64b (diff)
Revert "Add fixit for copyable-polymorphic"upstream/1.8
This reverts commit d6b07989e7a9211b21ecb9b9f8bd4d237bcc453e. It's causing a 4x slowdown. The culprit is running AccessSpecifierManager::VisitDeclaration for all classes, instead of QObject only Will reapply in master with a possible workaround CCBUG: 430959
-rw-r--r--src/AccessSpecifierManager.cpp19
-rw-r--r--src/AccessSpecifierManager.h3
-rw-r--r--src/checks/level2/copyable-polymorphic.cpp55
-rw-r--r--src/checks/level2/copyable-polymorphic.h1
-rw-r--r--tests/copyable-polymorphic/config.json5
-rw-r--r--tests/copyable-polymorphic/main.cpp9
-rw-r--r--tests/copyable-polymorphic/main.cpp.expected1
-rw-r--r--tests/copyable-polymorphic/main.cpp.fixed.expected72
8 files changed, 3 insertions, 162 deletions
diff --git a/src/AccessSpecifierManager.cpp b/src/AccessSpecifierManager.cpp
index 8b5b57b9..390478c8 100644
--- a/src/AccessSpecifierManager.cpp
+++ b/src/AccessSpecifierManager.cpp
@@ -167,7 +167,7 @@ const CXXRecordDecl *AccessSpecifierManager::classDefinitionForLoc(SourceLocatio
void AccessSpecifierManager::VisitDeclaration(Decl *decl)
{
auto record = dyn_cast<CXXRecordDecl>(decl);
- if (!record)
+ if (!clazy::isQObject(record))
return;
const auto &sm = m_ci.getSourceManager();
@@ -177,7 +177,7 @@ void AccessSpecifierManager::VisitDeclaration(Decl *decl)
auto it = m_preprocessorCallbacks->m_qtAccessSpecifiers.begin();
while (it != m_preprocessorCallbacks->m_qtAccessSpecifiers.end()) {
- if (classDefinitionForLoc((*it).loc) == record) {
+ if (classDefinitionForLoc((*it).loc) == record) {
sorted_insert(specifiers, *it, sm);
it = m_preprocessorCallbacks->m_qtAccessSpecifiers.erase(it);
} else {
@@ -286,18 +286,3 @@ llvm::StringRef AccessSpecifierManager::qtAccessSpecifierTypeStr(QtAccessSpecifi
return "";
}
-
-SourceLocation AccessSpecifierManager::firstLocationOfSection(
- AccessSpecifier specifier, clang::CXXRecordDecl *decl) const {
-
- auto it = m_specifiersMap.find(decl);
- if (it == m_specifiersMap.end())
- return {};
-
- for (const auto &record : it->second) {
- if (record.accessSpecifier == specifier) {
- return record.loc;
- }
- }
- return {};
-}
diff --git a/src/AccessSpecifierManager.h b/src/AccessSpecifierManager.h
index 3a246d34..4fb6fa48 100644
--- a/src/AccessSpecifierManager.h
+++ b/src/AccessSpecifierManager.h
@@ -97,9 +97,6 @@ public:
*/
llvm::StringRef qtAccessSpecifierTypeStr(QtAccessSpecifierType) const;
- clang::SourceLocation firstLocationOfSection(clang::AccessSpecifier specifier,
- clang::CXXRecordDecl *decl) const;
-
private:
ClazySpecifierList &entryForClassDefinition(clang::CXXRecordDecl*);
const clang::CompilerInstance &m_ci;
diff --git a/src/checks/level2/copyable-polymorphic.cpp b/src/checks/level2/copyable-polymorphic.cpp
index a234eade..b7e8bdfb 100644
--- a/src/checks/level2/copyable-polymorphic.cpp
+++ b/src/checks/level2/copyable-polymorphic.cpp
@@ -22,10 +22,6 @@
#include "copyable-polymorphic.h"
#include "Utils.h"
#include "SourceCompatibilityHelpers.h"
-#include "AccessSpecifierManager.h"
-#include "ClazyContext.h"
-#include "FixItUtils.h"
-#include "StringUtils.h"
#include <clang/AST/DeclCXX.h>
#include <clang/Basic/LLVM.h>
@@ -44,7 +40,6 @@ using namespace std;
CopyablePolymorphic::CopyablePolymorphic(const std::string &name, ClazyContext *context)
: CheckBase(name, context)
{
- context->enableAccessSpecifierManager();
}
void CopyablePolymorphic::VisitDecl(clang::Decl *decl)
@@ -62,53 +57,5 @@ void CopyablePolymorphic::VisitDecl(clang::Decl *decl)
return;
}
- emitWarning(clazy::getLocStart(record), "Polymorphic class " + record->getQualifiedNameAsString() + " is copyable. Potential slicing.", fixits(record));
-}
-
-vector<clang::FixItHint> CopyablePolymorphic::fixits(clang::CXXRecordDecl *record)
-{
- vector<FixItHint> result;
-
-#if LLVM_VERSION_MAJOR >= 11 // older llvm has problems with \n in the yaml file
- const StringRef className = clazy::name(record);
-
- // Insert Q_DISABLE_COPY(classname) in the private section if one exists,
- // otherwise at the end of the class declaration
- SourceLocation pos =
- m_context->accessSpecifierManager->firstLocationOfSection(
- clang::AccessSpecifier::AS_private, record);
-
- if (pos.isValid()) {
- pos = Lexer::findLocationAfterToken(pos, clang::tok::colon, sm(), lo(),
- false);
- result.push_back(clazy::createInsertion(
- pos, string("\n\tQ_DISABLE_COPY(") + className.data() + string(")")));
- } else {
- pos = record->getBraceRange().getEnd();
- result.push_back(clazy::createInsertion(
- pos, string("\tQ_DISABLE_COPY(") + className.data() + string(")\n")));
- }
-
- // If the class has a default constructor, then we need to readd it,
- // as the disabled copy constructor removes it.
- // Add it in the public section if one exists, otherwise add a
- // public section at the top of the class declaration.
- if (record->hasDefaultConstructor()) {
- pos = m_context->accessSpecifierManager->firstLocationOfSection(
- clang::AccessSpecifier::AS_public, record);
- if (pos.isInvalid()) {
- pos = record->getBraceRange().getBegin().getLocWithOffset(1);
- result.push_back(clazy::createInsertion(
- pos, string("\npublic:\n\t") + className.data() + string("() = default;")));
- }
- else {
- pos = Lexer::findLocationAfterToken(pos, clang::tok::colon, sm(), lo(),
- false);
- result.push_back(clazy::createInsertion(
- pos, string("\n\t") + className.data() + string("() = default;")));
- }
- }
-#endif
-
- return result;
+ emitWarning(clazy::getLocStart(record), "Polymorphic class " + record->getQualifiedNameAsString() + " is copyable. Potential slicing.");
}
diff --git a/src/checks/level2/copyable-polymorphic.h b/src/checks/level2/copyable-polymorphic.h
index ade22be2..6fd051ab 100644
--- a/src/checks/level2/copyable-polymorphic.h
+++ b/src/checks/level2/copyable-polymorphic.h
@@ -44,7 +44,6 @@ class CopyablePolymorphic
public:
explicit CopyablePolymorphic(const std::string &name, ClazyContext *context);
void VisitDecl(clang::Decl *) override;
- vector<clang::FixItHint> fixits(clang::CXXRecordDecl* record);
};
#endif
diff --git a/tests/copyable-polymorphic/config.json b/tests/copyable-polymorphic/config.json
index 0924f92c..e7e6e0cb 100644
--- a/tests/copyable-polymorphic/config.json
+++ b/tests/copyable-polymorphic/config.json
@@ -2,11 +2,6 @@
"tests" : [
{
"filename" : "main.cpp"
- },
- {
- "filename" : "fixits.cpp",
- "has_fixits" : true,
- "minimum_clang_version" : 1100
}
]
}
diff --git a/tests/copyable-polymorphic/main.cpp b/tests/copyable-polymorphic/main.cpp
index 352766d0..b4bbec4b 100644
--- a/tests/copyable-polymorphic/main.cpp
+++ b/tests/copyable-polymorphic/main.cpp
@@ -52,12 +52,3 @@ struct DerivedFromNotCopyable : public PolymorphicClass3 // OK, not copyable
struct DerivedFromCopyable : public PolymorphicClass4 // Warning, copyable
{
};
-
-class ClassWithPrivateSection
-{
-public:
- virtual void foo();
-private:
- void bar();
- int i;
-};
diff --git a/tests/copyable-polymorphic/main.cpp.expected b/tests/copyable-polymorphic/main.cpp.expected
index 44dc9227..074b6ce5 100644
--- a/tests/copyable-polymorphic/main.cpp.expected
+++ b/tests/copyable-polymorphic/main.cpp.expected
@@ -1,4 +1,3 @@
copyable-polymorphic/main.cpp:36:1: warning: Polymorphic class PolymorphicClass4 is copyable. Potential slicing. [-Wclazy-copyable-polymorphic]
copyable-polymorphic/main.cpp:43:1: warning: Polymorphic class PolymorphicClass5 is copyable. Potential slicing. [-Wclazy-copyable-polymorphic]
copyable-polymorphic/main.cpp:52:1: warning: Polymorphic class DerivedFromCopyable is copyable. Potential slicing. [-Wclazy-copyable-polymorphic]
-copyable-polymorphic/main.cpp:56:1: warning: Polymorphic class ClassWithPrivateSection is copyable. Potential slicing. [-Wclazy-copyable-polymorphic]
diff --git a/tests/copyable-polymorphic/main.cpp.fixed.expected b/tests/copyable-polymorphic/main.cpp.fixed.expected
deleted file mode 100644
index 1821564d..00000000
--- a/tests/copyable-polymorphic/main.cpp.fixed.expected
+++ /dev/null
@@ -1,72 +0,0 @@
-
-
-struct ValueClass // OK
-{
- int m;
-};
-
-struct DerivedValueClass : public ValueClass // OK
-{
- int m;
-};
-
-struct PolymorphicClass1 // OK, not copyable
-{
- virtual void foo();
- PolymorphicClass1(const PolymorphicClass1 &) = delete;
- PolymorphicClass1& operator=(const PolymorphicClass1 &) = delete;
-};
-
-struct PolymorphicClass2 // OK, not copyable
-{
- virtual void foo();
-private:
- PolymorphicClass2(const PolymorphicClass2 &);
- PolymorphicClass2& operator=(const PolymorphicClass2 &);
-};
-
-struct PolymorphicClass3 // OK, not copyable
-{
- virtual void foo();
- PolymorphicClass3(const PolymorphicClass3 &) = delete;
-private:
- PolymorphicClass3& operator=(const PolymorphicClass3 &) = delete;
-};
-
-struct PolymorphicClass4 // Warning, copyable
-{
- virtual void foo();
- PolymorphicClass4(const PolymorphicClass4 &);
- PolymorphicClass4& operator=(const PolymorphicClass4 &);
- Q_DISABLE_COPY(PolymorphicClass4)
-};
-
-struct PolymorphicClass5 // Warning, copyable
-{
-public:
- PolymorphicClass5() = default;
- virtual void foo();
- Q_DISABLE_COPY(PolymorphicClass5)
-};
-
-struct DerivedFromNotCopyable : public PolymorphicClass3 // OK, not copyable
-{
-};
-
-struct DerivedFromCopyable : public PolymorphicClass4 // Warning, copyable
-{
-public:
- DerivedFromCopyable() = default;
- Q_DISABLE_COPY(DerivedFromCopyable)
-};
-
-class ClassWithPrivateSection
-{
-public:
- ClassWithPrivateSection() = default;
- virtual void foo();
-private:
- Q_DISABLE_COPY(ClassWithPrivateSection)
- void bar();
- int i;
-};