2015-11-17 21:50:45 +01: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/.
|
|
|
|
*/
|
|
|
|
|
2016-06-28 18:54:31 +02:00
|
|
|
#ifndef INCLUDED_COMPILERPLUGINS_CLANG_CHECK_HXX
|
|
|
|
#define INCLUDED_COMPILERPLUGINS_CLANG_CHECK_HXX
|
2015-11-17 21:50:45 +01:00
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
|
|
|
#include <clang/AST/DeclBase.h>
|
2015-12-08 22:56:02 +01:00
|
|
|
#include <clang/AST/Decl.h>
|
2015-11-17 21:50:45 +01:00
|
|
|
#include <clang/AST/Type.h>
|
2016-06-28 18:54:31 +02:00
|
|
|
#include <clang/Basic/OperatorKinds.h>
|
2015-11-17 21:50:45 +01:00
|
|
|
|
|
|
|
namespace loplugin {
|
|
|
|
|
2016-06-28 18:54:31 +02:00
|
|
|
class ContextCheck;
|
2015-12-08 22:56:02 +01:00
|
|
|
class TerminalCheck;
|
2015-11-17 21:50:45 +01:00
|
|
|
|
2016-06-28 17:48:22 +02:00
|
|
|
namespace detail {
|
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
inline ContextCheck checkRecordDecl(
|
|
|
|
clang::Decl const * decl, clang::TagTypeKind tag, llvm::StringRef id);
|
2016-06-28 17:48:22 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-17 21:50:45 +01:00
|
|
|
class TypeCheck {
|
|
|
|
public:
|
|
|
|
explicit TypeCheck(clang::QualType type): type_(type) {}
|
|
|
|
|
2019-06-06 09:54:31 +02:00
|
|
|
explicit TypeCheck(clang::Type const * type): type_(type, 0) {}
|
|
|
|
|
2017-01-26 15:36:47 +02:00
|
|
|
explicit TypeCheck(clang::TypeDecl const * decl): type_(decl->getTypeForDecl(), 0) {}
|
|
|
|
|
2015-11-17 21:50:45 +01:00
|
|
|
explicit operator bool() const { return !type_.isNull(); }
|
|
|
|
|
2017-07-14 09:02:11 +02:00
|
|
|
TypeCheck NonConst() const;
|
|
|
|
|
2016-12-18 13:52:04 +01:00
|
|
|
TypeCheck NonConstVolatile() const;
|
|
|
|
|
2015-12-08 22:56:02 +01:00
|
|
|
TypeCheck Const() const;
|
2015-11-17 21:50:45 +01:00
|
|
|
|
2016-12-18 13:52:04 +01:00
|
|
|
TypeCheck Volatile() const;
|
|
|
|
|
|
|
|
TypeCheck ConstVolatile() const;
|
|
|
|
|
|
|
|
TerminalCheck Void() const;
|
|
|
|
|
2015-12-08 22:56:02 +01:00
|
|
|
TerminalCheck Char() const;
|
|
|
|
|
2016-06-19 21:29:43 +02:00
|
|
|
TerminalCheck AnyBoolean() const;
|
|
|
|
|
2016-06-03 10:58:26 +02:00
|
|
|
TypeCheck Pointer() const;
|
|
|
|
|
2017-03-20 09:01:33 +02:00
|
|
|
TerminalCheck Enum() const;
|
|
|
|
|
2015-12-08 22:56:02 +01:00
|
|
|
TypeCheck LvalueReference() const;
|
2015-11-17 21:50:45 +01:00
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
inline ContextCheck Class(llvm::StringRef id) const;
|
2015-11-17 21:50:45 +01:00
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
inline ContextCheck Struct(llvm::StringRef id) const;
|
2017-06-19 09:32:45 +02:00
|
|
|
|
2016-07-08 16:46:57 +02:00
|
|
|
TypeCheck Typedef() const;
|
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
inline ContextCheck Typedef(llvm::StringRef id) const;
|
2016-07-08 16:46:57 +02:00
|
|
|
|
2015-12-08 22:56:02 +01:00
|
|
|
TypeCheck NotSubstTemplateTypeParmType() const;
|
|
|
|
|
2015-11-17 21:50:45 +01:00
|
|
|
private:
|
|
|
|
TypeCheck() = default;
|
|
|
|
|
2017-10-22 11:44:13 +02:00
|
|
|
clang::QualType const type_{};
|
2015-11-17 21:50:45 +01:00
|
|
|
};
|
|
|
|
|
2016-06-28 18:54:31 +02:00
|
|
|
class DeclCheck {
|
|
|
|
public:
|
|
|
|
explicit DeclCheck(clang::Decl const * decl): decl_(decl) {}
|
|
|
|
|
|
|
|
explicit operator bool() const { return decl_ != nullptr; }
|
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
inline ContextCheck Class(llvm::StringRef id) const;
|
2016-06-28 18:54:31 +02:00
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
inline ContextCheck Struct(llvm::StringRef id) const;
|
2016-06-28 18:54:31 +02:00
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
inline ContextCheck Union(llvm::StringRef id) const;
|
2017-06-30 11:16:03 +02:00
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
inline ContextCheck Function(llvm::StringRef id) const;
|
2016-06-28 18:54:31 +02:00
|
|
|
|
|
|
|
ContextCheck Operator(clang::OverloadedOperatorKind op) const;
|
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
inline ContextCheck Var(llvm::StringRef id) const;
|
2016-10-08 23:49:29 +02:00
|
|
|
|
2016-06-29 11:31:13 +02:00
|
|
|
ContextCheck MemberFunction() const;
|
|
|
|
|
2016-06-28 18:54:31 +02:00
|
|
|
private:
|
|
|
|
clang::Decl const * const decl_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ContextCheck {
|
2015-11-17 21:50:45 +01:00
|
|
|
public:
|
2019-08-28 11:17:20 +02:00
|
|
|
explicit ContextCheck(clang::DeclContext const * context = nullptr):
|
|
|
|
context_(context) {}
|
|
|
|
|
2015-11-17 21:50:45 +01:00
|
|
|
explicit operator bool() const { return context_ != nullptr; }
|
|
|
|
|
2015-12-08 22:56:02 +01:00
|
|
|
TerminalCheck GlobalNamespace() const;
|
2015-11-17 21:50:45 +01:00
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
inline ContextCheck Namespace(llvm::StringRef id) const;
|
2015-11-17 21:50:45 +01:00
|
|
|
|
2015-12-08 22:56:02 +01:00
|
|
|
TerminalCheck StdNamespace() const;
|
2015-11-17 21:50:45 +01:00
|
|
|
|
2019-08-28 11:17:20 +02:00
|
|
|
TerminalCheck StdOrNestedNamespace() const;
|
|
|
|
|
2016-06-28 18:54:31 +02:00
|
|
|
ContextCheck AnonymousNamespace() const;
|
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
inline ContextCheck Class(llvm::StringRef id) const;
|
2016-06-28 18:54:31 +02:00
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
inline ContextCheck Struct(llvm::StringRef id) const;
|
2016-06-28 17:48:22 +02:00
|
|
|
|
2019-03-07 14:31:17 +01:00
|
|
|
explicit ContextCheck(const clang::NamespaceDecl * decl ) : context_( decl ) {}
|
|
|
|
|
2015-11-17 21:50:45 +01:00
|
|
|
private:
|
|
|
|
clang::DeclContext const * const context_;
|
|
|
|
};
|
|
|
|
|
2015-12-08 22:56:02 +01:00
|
|
|
class TerminalCheck {
|
|
|
|
public:
|
|
|
|
explicit operator bool() const { return satisfied_; }
|
|
|
|
|
|
|
|
private:
|
2016-06-28 18:54:31 +02:00
|
|
|
friend ContextCheck;
|
2015-12-08 22:56:02 +01:00
|
|
|
friend TypeCheck;
|
|
|
|
|
|
|
|
explicit TerminalCheck(bool satisfied): satisfied_(satisfied) {}
|
|
|
|
|
|
|
|
bool const satisfied_;
|
|
|
|
};
|
|
|
|
|
2019-03-06 14:39:13 +01:00
|
|
|
|
|
|
|
typedef std::function<bool(clang::Decl const *)> DeclChecker;
|
|
|
|
// Returns true if the class has a base matching the checker, or if the class itself matches.
|
|
|
|
bool isDerivedFrom(const clang::CXXRecordDecl *decl, DeclChecker base);
|
|
|
|
|
|
|
|
|
2016-06-28 17:48:22 +02:00
|
|
|
namespace detail {
|
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
ContextCheck checkRecordDecl(
|
|
|
|
clang::Decl const * decl, clang::TagTypeKind tag, llvm::StringRef id)
|
2016-06-28 17:48:22 +02:00
|
|
|
{
|
|
|
|
auto r = llvm::dyn_cast_or_null<clang::RecordDecl>(decl);
|
|
|
|
if (r != nullptr && r->getTagKind() == tag) {
|
|
|
|
auto const i = r->getIdentifier();
|
2018-11-29 08:46:47 +02:00
|
|
|
if (i != nullptr && i->getName() == id) {
|
2016-06-28 18:54:31 +02:00
|
|
|
return ContextCheck(r->getDeclContext());
|
2016-06-28 17:48:22 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-28 18:54:31 +02:00
|
|
|
return ContextCheck();
|
2016-06-28 17:48:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
ContextCheck TypeCheck::Class(llvm::StringRef id)
|
2015-11-17 21:50:45 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
if (!type_.isNull()) {
|
2015-12-08 22:56:02 +01:00
|
|
|
auto const t = type_->getAs<clang::RecordType>();
|
2015-11-17 21:50:45 +01:00
|
|
|
if (t != nullptr) {
|
2016-06-28 17:48:22 +02:00
|
|
|
return detail::checkRecordDecl(t->getDecl(), clang::TTK_Class, id);
|
2015-11-17 21:50:45 +01:00
|
|
|
}
|
|
|
|
}
|
2016-06-28 18:54:31 +02:00
|
|
|
return ContextCheck();
|
|
|
|
}
|
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
ContextCheck TypeCheck::Struct(llvm::StringRef id) const
|
2017-06-19 09:32:45 +02:00
|
|
|
{
|
|
|
|
if (!type_.isNull()) {
|
|
|
|
auto const t = type_->getAs<clang::RecordType>();
|
|
|
|
if (t != nullptr) {
|
|
|
|
return detail::checkRecordDecl(t->getDecl(), clang::TTK_Struct, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ContextCheck();
|
|
|
|
}
|
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
ContextCheck TypeCheck::Typedef(llvm::StringRef id) const
|
2016-07-08 16:46:57 +02:00
|
|
|
{
|
|
|
|
if (!type_.isNull()) {
|
|
|
|
if (auto const t = type_->getAs<clang::TypedefType>()) {
|
|
|
|
auto const d = t->getDecl();
|
|
|
|
auto const i = d->getIdentifier();
|
|
|
|
assert(i != nullptr);
|
2018-11-29 08:46:47 +02:00
|
|
|
if (i->getName() == id) {
|
2016-07-08 16:46:57 +02:00
|
|
|
return ContextCheck(d->getDeclContext());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ContextCheck();
|
|
|
|
}
|
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
ContextCheck DeclCheck::Class(llvm::StringRef id) const
|
2016-06-28 18:54:31 +02:00
|
|
|
{
|
|
|
|
return detail::checkRecordDecl(decl_, clang::TTK_Class, id);
|
2015-11-17 21:50:45 +01:00
|
|
|
}
|
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
ContextCheck DeclCheck::Struct(llvm::StringRef id) const
|
2016-06-28 18:54:31 +02:00
|
|
|
{
|
|
|
|
return detail::checkRecordDecl(decl_, clang::TTK_Struct, id);
|
|
|
|
}
|
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
ContextCheck DeclCheck::Union(llvm::StringRef id) const
|
2017-06-30 11:16:03 +02:00
|
|
|
{
|
|
|
|
return detail::checkRecordDecl(decl_, clang::TTK_Union, id);
|
|
|
|
}
|
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
ContextCheck DeclCheck::Function(llvm::StringRef id) const
|
2016-06-28 18:54:31 +02:00
|
|
|
{
|
|
|
|
auto f = llvm::dyn_cast_or_null<clang::FunctionDecl>(decl_);
|
2016-10-08 23:49:29 +02:00
|
|
|
if (f != nullptr) {
|
|
|
|
auto const i = f->getIdentifier();
|
2018-11-29 08:46:47 +02:00
|
|
|
if (i != nullptr && i->getName() == id) {
|
2016-10-08 23:49:29 +02:00
|
|
|
return ContextCheck(f->getDeclContext());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ContextCheck();
|
|
|
|
}
|
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
ContextCheck DeclCheck::Var(llvm::StringRef id) const
|
2016-10-08 23:49:29 +02:00
|
|
|
{
|
|
|
|
auto f = llvm::dyn_cast_or_null<clang::VarDecl>(decl_);
|
2016-06-28 18:54:31 +02:00
|
|
|
if (f != nullptr) {
|
|
|
|
auto const i = f->getIdentifier();
|
2018-11-29 08:46:47 +02:00
|
|
|
if (i != nullptr && i->getName() == id) {
|
2016-06-28 18:54:31 +02:00
|
|
|
return ContextCheck(f->getDeclContext());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ContextCheck();
|
|
|
|
}
|
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
ContextCheck ContextCheck::Namespace(llvm::StringRef id) const
|
2015-12-08 22:56:02 +01:00
|
|
|
{
|
|
|
|
if (context_) {
|
|
|
|
auto n = llvm::dyn_cast<clang::NamespaceDecl>(context_);
|
|
|
|
if (n != nullptr) {
|
|
|
|
auto const i = n->getIdentifier();
|
2018-11-29 08:46:47 +02:00
|
|
|
if (i != nullptr && i->getName() == id) {
|
2016-06-28 18:54:31 +02:00
|
|
|
return ContextCheck(n->getParent());
|
2015-12-08 22:56:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-28 18:54:31 +02:00
|
|
|
return ContextCheck();
|
2015-12-08 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
ContextCheck ContextCheck::Class(llvm::StringRef id) const
|
2016-06-28 17:48:22 +02:00
|
|
|
{
|
2016-06-28 18:54:31 +02:00
|
|
|
return detail::checkRecordDecl(
|
|
|
|
llvm::dyn_cast_or_null<clang::Decl>(context_), clang::TTK_Class, id);
|
|
|
|
}
|
|
|
|
|
2018-11-29 08:46:47 +02:00
|
|
|
ContextCheck ContextCheck::Struct(llvm::StringRef id) const
|
2016-06-28 18:54:31 +02:00
|
|
|
{
|
|
|
|
return detail::checkRecordDecl(
|
|
|
|
llvm::dyn_cast_or_null<clang::Decl>(context_), clang::TTK_Struct, id);
|
2016-06-28 17:48:22 +02:00
|
|
|
}
|
|
|
|
|
2017-07-03 12:34:38 +02:00
|
|
|
bool isExtraWarnUnusedType(clang::QualType type);
|
|
|
|
|
2018-03-23 13:18:34 +01:00
|
|
|
bool isOkToRemoveArithmeticCast(
|
|
|
|
clang::ASTContext & context, clang::QualType t1, clang::QualType t2,
|
|
|
|
const clang::Expr* subExpr);
|
|
|
|
|
2015-11-17 21:50:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|