2022-03-10 06:41:47 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
|
|
|
|
#include "plugin.hxx"
|
|
|
|
#include "check.hxx"
|
|
|
|
#include "config_clang.h"
|
|
|
|
#include "clang/AST/CXXInheritance.h"
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
|
|
|
|
|
|
|
// Look for explicit destructors that can be trivial (and therefore don't need to be declared)
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
class TrivialDestructor : public loplugin::FilteringPlugin<TrivialDestructor>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit TrivialDestructor(loplugin::InstantiationData const& data)
|
|
|
|
: FilteringPlugin(data)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
|
|
|
|
|
|
|
|
bool VisitCXXDestructorDecl(CXXDestructorDecl const*);
|
|
|
|
|
|
|
|
private:
|
2022-03-14 16:25:23 -05:00
|
|
|
bool HasTrivialDestructorBody(const CXXRecordDecl* ClassDecl);
|
|
|
|
bool FieldHasTrivialDestructor(const FieldDecl* Field);
|
2022-03-10 06:41:47 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
bool TrivialDestructor::VisitCXXDestructorDecl(CXXDestructorDecl const* destructorDecl)
|
|
|
|
{
|
|
|
|
if (ignoreLocation(destructorDecl))
|
|
|
|
return true;
|
2022-03-14 15:33:31 -05:00
|
|
|
if (!destructorDecl->isThisDeclarationADefinition())
|
|
|
|
return true;
|
2022-03-10 06:41:47 -06:00
|
|
|
if (!destructorDecl->hasTrivialBody())
|
|
|
|
return true;
|
|
|
|
if (destructorDecl->isVirtual())
|
|
|
|
return true;
|
|
|
|
if (destructorDecl->getExceptionSpecType() != EST_None)
|
|
|
|
return true;
|
|
|
|
if (destructorDecl->getAccess() != AS_public)
|
|
|
|
return true;
|
|
|
|
if (isInUnoIncludeFile(
|
|
|
|
compiler.getSourceManager().getSpellingLoc(destructorDecl->getLocation())))
|
|
|
|
return true;
|
2022-03-14 16:25:23 -05:00
|
|
|
if (!HasTrivialDestructorBody(destructorDecl->getParent()))
|
2022-03-10 06:41:47 -06:00
|
|
|
return true;
|
|
|
|
|
|
|
|
report(DiagnosticsEngine::Warning, "no need for explicit destructor decl",
|
|
|
|
destructorDecl->getLocation())
|
|
|
|
<< destructorDecl->getSourceRange();
|
2022-03-14 15:33:31 -05:00
|
|
|
for (FunctionDecl const* d2 = destructorDecl;;)
|
2022-03-10 06:41:47 -06:00
|
|
|
{
|
2022-03-14 15:33:31 -05:00
|
|
|
d2 = d2->getPreviousDecl();
|
|
|
|
if (d2 == nullptr)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
report(DiagnosticsEngine::Note, "previous declaration is here", d2->getLocation())
|
|
|
|
<< d2->getSourceRange();
|
2022-03-10 06:41:47 -06:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-14 16:25:23 -05:00
|
|
|
bool TrivialDestructor::HasTrivialDestructorBody(const CXXRecordDecl* ClassDecl)
|
2022-03-10 06:41:47 -06:00
|
|
|
{
|
|
|
|
// Check fields.
|
2022-03-14 16:25:23 -05:00
|
|
|
for (const auto* field : ClassDecl->fields())
|
|
|
|
if (!FieldHasTrivialDestructor(field))
|
2022-03-10 06:41:47 -06:00
|
|
|
return false;
|
|
|
|
|
2022-03-14 16:25:23 -05:00
|
|
|
// Check bases.
|
|
|
|
for (const auto& I : ClassDecl->bases())
|
2022-03-10 06:41:47 -06:00
|
|
|
{
|
2022-03-14 16:25:23 -05:00
|
|
|
const CXXRecordDecl* Base = I.getType()->getAsCXXRecordDecl();
|
|
|
|
if (!Base->hasTrivialDestructor())
|
2022-03-10 06:41:47 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-14 16:25:23 -05:00
|
|
|
bool TrivialDestructor::FieldHasTrivialDestructor(const FieldDecl* Field)
|
2022-03-10 06:41:47 -06:00
|
|
|
{
|
|
|
|
QualType FieldBaseElementType = compiler.getASTContext().getBaseElementType(Field->getType());
|
|
|
|
|
|
|
|
const RecordType* RT = FieldBaseElementType->getAs<RecordType>();
|
|
|
|
if (!RT)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
CXXRecordDecl* FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
|
|
|
|
return FieldClassDecl->hasTrivialDestructor();
|
|
|
|
}
|
|
|
|
|
|
|
|
loplugin::Plugin::Registration<TrivialDestructor> X("trivialdestructor", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|