LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / compilerplugins / clang / check.hxx
blob9c35acff7b5eaf973652919ae60933627f8ee1ef
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #pragma once
12 #include <cstddef>
14 #include <clang/AST/DeclBase.h>
15 #include <clang/AST/Decl.h>
16 #include <clang/AST/Type.h>
17 #include <clang/Basic/OperatorKinds.h>
19 namespace loplugin {
21 class ContextCheck;
22 class TerminalCheck;
24 namespace detail {
26 inline ContextCheck checkRecordDecl(
27 clang::Decl const * decl, clang::TagTypeKind tag, llvm::StringRef id);
31 class DeclCheck;
33 class TypeCheck {
34 public:
35 explicit TypeCheck(clang::QualType type): type_(type) {}
37 explicit TypeCheck(clang::Type const * type): type_(type, 0) {}
39 explicit TypeCheck(clang::TypeDecl const * decl): type_(decl->getTypeForDecl(), 0) {}
41 explicit operator bool() const { return !type_.isNull(); }
43 TypeCheck NonConst() const;
45 TypeCheck NonConstVolatile() const;
47 TypeCheck Const() const;
49 TypeCheck Volatile() const;
51 TypeCheck ConstVolatile() const;
53 TypeCheck ConstNonVolatile() const;
55 TerminalCheck Void() const;
57 TerminalCheck Char() const;
59 TerminalCheck AnyBoolean() const;
61 TypeCheck Pointer() const;
63 TerminalCheck Enum() const;
65 TypeCheck LvalueReference() const;
67 TypeCheck RvalueReference() const;
69 inline ContextCheck Class(llvm::StringRef id) const;
71 inline ContextCheck Struct(llvm::StringRef id) const;
73 inline ContextCheck ClassOrStruct(llvm::StringRef id) const;
75 TypeCheck Typedef() const;
77 inline ContextCheck Typedef(llvm::StringRef id) const;
79 DeclCheck TemplateSpecializationClass() const;
81 TypeCheck NotSubstTemplateTypeParmType() const;
83 private:
84 TypeCheck() = default;
86 clang::QualType const type_{};
89 class DeclCheck {
90 friend TypeCheck;
92 public:
93 explicit DeclCheck(clang::Decl const * decl): decl_(decl) {}
95 explicit operator bool() const { return decl_ != nullptr; }
97 inline ContextCheck Class(llvm::StringRef id) const;
99 inline ContextCheck Struct(llvm::StringRef id) const;
101 inline ContextCheck ClassOrStruct(llvm::StringRef id) const;
103 inline ContextCheck Union(llvm::StringRef id) const;
105 inline ContextCheck Function(llvm::StringRef id) const;
107 ContextCheck Operator(clang::OverloadedOperatorKind op) const;
109 inline ContextCheck Var(llvm::StringRef id) const;
111 ContextCheck MemberFunction() const;
113 private:
114 DeclCheck() = default;
116 clang::Decl const * const decl_ = nullptr;
119 class ContextCheck {
120 public:
121 explicit ContextCheck(clang::DeclContext const * context = nullptr):
122 context_(context) {}
124 explicit operator bool() const { return context_ != nullptr; }
126 TerminalCheck GlobalNamespace() const;
128 inline ContextCheck Namespace(llvm::StringRef id) const;
130 TerminalCheck StdNamespace() const;
132 TerminalCheck StdOrNestedNamespace() const;
134 ContextCheck AnonymousNamespace() const;
136 inline ContextCheck Class(llvm::StringRef id) const;
138 inline ContextCheck Struct(llvm::StringRef id) const;
140 explicit ContextCheck(const clang::NamespaceDecl * decl ) : context_( decl ) {}
142 private:
143 clang::DeclContext const * const context_;
146 class TerminalCheck {
147 public:
148 explicit operator bool() const { return satisfied_; }
150 private:
151 friend ContextCheck;
152 friend TypeCheck;
154 explicit TerminalCheck(bool satisfied): satisfied_(satisfied) {}
156 bool const satisfied_;
160 typedef std::function<bool(clang::Decl const *)> DeclChecker;
161 // Returns true if the class has a base matching the checker, or if the class itself matches.
162 bool isDerivedFrom(const clang::CXXRecordDecl *decl, DeclChecker base);
165 namespace detail {
167 ContextCheck checkRecordDecl(
168 clang::Decl const * decl, clang::TagTypeKind tag, llvm::StringRef id)
170 auto r = llvm::dyn_cast_or_null<clang::RecordDecl>(decl);
171 if (r != nullptr && r->getTagKind() == tag) {
172 auto const i = r->getIdentifier();
173 if (i != nullptr && i->getName() == id) {
174 return ContextCheck(r->getDeclContext());
177 return ContextCheck();
182 ContextCheck TypeCheck::Class(llvm::StringRef id)
183 const
185 if (!type_.isNull()) {
186 auto const t = type_->getAs<clang::RecordType>();
187 if (t != nullptr) {
188 return detail::checkRecordDecl(t->getDecl(), clang::TTK_Class, id);
191 return ContextCheck();
194 ContextCheck TypeCheck::Struct(llvm::StringRef id) const
196 if (!type_.isNull()) {
197 auto const t = type_->getAs<clang::RecordType>();
198 if (t != nullptr) {
199 return detail::checkRecordDecl(t->getDecl(), clang::TTK_Struct, id);
202 return ContextCheck();
205 ContextCheck TypeCheck::ClassOrStruct(llvm::StringRef id) const
207 auto const c1 = Class(id);
208 if (c1) {
209 return c1;
211 return Struct(id);
214 ContextCheck TypeCheck::Typedef(llvm::StringRef id) const
216 if (!type_.isNull()) {
217 if (auto const t = type_->getAs<clang::TypedefType>()) {
218 auto const d = t->getDecl();
219 auto const i = d->getIdentifier();
220 assert(i != nullptr);
221 if (i->getName() == id) {
222 return ContextCheck(d->getDeclContext());
226 return ContextCheck();
229 ContextCheck DeclCheck::Class(llvm::StringRef id) const
231 return detail::checkRecordDecl(decl_, clang::TTK_Class, id);
234 ContextCheck DeclCheck::Struct(llvm::StringRef id) const
236 return detail::checkRecordDecl(decl_, clang::TTK_Struct, id);
239 ContextCheck DeclCheck::ClassOrStruct(llvm::StringRef id) const
241 auto const c1 = Class(id);
242 if (c1) {
243 return c1;
245 return Struct(id);
248 ContextCheck DeclCheck::Union(llvm::StringRef id) const
250 return detail::checkRecordDecl(decl_, clang::TTK_Union, id);
253 ContextCheck DeclCheck::Function(llvm::StringRef id) const
255 auto f = llvm::dyn_cast_or_null<clang::FunctionDecl>(decl_);
256 if (f != nullptr) {
257 auto const i = f->getIdentifier();
258 if (i != nullptr && i->getName() == id) {
259 return ContextCheck(f->getDeclContext());
262 return ContextCheck();
265 ContextCheck DeclCheck::Var(llvm::StringRef id) const
267 auto f = llvm::dyn_cast_or_null<clang::VarDecl>(decl_);
268 if (f != nullptr) {
269 auto const i = f->getIdentifier();
270 if (i != nullptr && i->getName() == id) {
271 return ContextCheck(f->getDeclContext());
274 return ContextCheck();
277 ContextCheck ContextCheck::Namespace(llvm::StringRef id) const
279 if (context_) {
280 auto n = llvm::dyn_cast<clang::NamespaceDecl>(context_);
281 if (n != nullptr) {
282 auto const i = n->getIdentifier();
283 if (i != nullptr && i->getName() == id) {
284 return ContextCheck(n->getParent());
288 return ContextCheck();
291 ContextCheck ContextCheck::Class(llvm::StringRef id) const
293 return detail::checkRecordDecl(
294 llvm::dyn_cast_or_null<clang::Decl>(context_), clang::TTK_Class, id);
297 ContextCheck ContextCheck::Struct(llvm::StringRef id) const
299 return detail::checkRecordDecl(
300 llvm::dyn_cast_or_null<clang::Decl>(context_), clang::TTK_Struct, id);
303 bool isExtraWarnUnusedType(clang::QualType type);
305 bool isOkToRemoveArithmeticCast(
306 clang::ASTContext & context, clang::QualType t1, clang::QualType t2,
307 const clang::Expr* subExpr);
311 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */