bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / dynexcspec.cxx
blobe5e15cb139fc62b47c4ed3af2c7ef6c72c531c05
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 #include <algorithm>
11 #include <functional>
13 #include "clang/AST/Comment.h"
15 #include "plugin.hxx"
17 // Remove dynamic exception specifications. See the mail thread starting at
18 // <https://lists.freedesktop.org/archives/libreoffice/2017-January/076665.html>
19 // "Dynamic Exception Specifications" for details.
21 namespace {
23 bool isOverriding(FunctionDecl const * decl) {
24 if (decl->hasAttr<OverrideAttr>()) {
25 return true;
27 auto m = dyn_cast<CXXMethodDecl>(decl);
28 return m != nullptr
29 && m->begin_overridden_methods() != m->end_overridden_methods();
32 bool isDtorOrDealloc(FunctionDecl const * decl) {
33 if (isa<CXXDestructorDecl>(decl)) {
34 return true;
36 switch (decl->getOverloadedOperator()) {
37 case OO_Delete:
38 case OO_Array_Delete:
39 return true;
40 default:
41 return false;
45 class DynExcSpec:
46 public loplugin::FilteringRewritePlugin<DynExcSpec>
48 public:
49 explicit DynExcSpec(loplugin::InstantiationData const & data):
50 FilteringRewritePlugin(data) {}
52 void run() override {
53 if (compiler.getLangOpts().CPlusPlus) {
54 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
58 bool VisitFunctionDecl(FunctionDecl const * decl) {
59 if (ignoreLocation(decl)) {
60 return true;
62 auto proto = decl->getType()->getAs<FunctionProtoType>();
63 if (proto == nullptr || proto->getExceptionSpecType() != EST_Dynamic) {
64 return true;
66 if (decl->isCanonicalDecl() && !isOverriding(decl)
67 && !anyRedeclHasThrowsDocumentation(decl))
69 report(
70 DiagnosticsEngine::Warning,
71 ("function declaration has dynamic exception specification but"
72 " no corresponding documentation comment"),
73 decl->getLocation())
74 << decl->getSourceRange();
76 if (rewriter != nullptr) {
77 if (!(decl->isDefined() || decl->isPure())) {
78 return true;
80 if (auto m = dyn_cast<CXXMethodDecl>(decl)) {
81 for (auto i = m->begin_overridden_methods();
82 i != m->end_overridden_methods(); ++i)
84 auto proto2 = (*i)->getType()->getAs<FunctionProtoType>();
85 assert(proto2 != nullptr);
86 if (proto2->getExceptionSpecType() == EST_Dynamic) {
87 return true;
92 bool dtorOrDealloc = isDtorOrDealloc(decl);
93 auto const source = decl->getExceptionSpecSourceRange();
94 if (rewriter != nullptr && source.isValid()) {
95 if (dtorOrDealloc) {
96 if (replaceText(source, "noexcept(false)")) {
97 return true;
99 } else {
100 auto beg = source.getBegin();
101 if (beg.isFileID()) {
102 for (;;) {
103 auto prev = Lexer::GetBeginningOfToken(
104 beg.getLocWithOffset(-1),
105 compiler.getSourceManager(),
106 compiler.getLangOpts());
107 auto n = Lexer::MeasureTokenLength(
108 prev, compiler.getSourceManager(),
109 compiler.getLangOpts());
110 auto s = StringRef(
111 compiler.getSourceManager().getCharacterData(prev),
113 while (s.startswith("\\\n")) {
114 s = s.drop_front(2);
115 while (!s.empty()
116 && (s.front() == ' ' || s.front() == '\t'
117 || s.front() == '\n' || s.front() == '\v'
118 || s.front() == '\f'))
120 s = s.drop_front(1);
123 if (!s.empty() && s != "\\") {
124 if (s.startswith("//")) {
125 beg = source.getBegin();
127 break;
129 beg = prev;
132 if (removeText(SourceRange(beg, source.getEnd()))) {
133 return true;
137 report(
138 DiagnosticsEngine::Warning,
139 (dtorOrDealloc
140 ? "replace dynamic exception specification with 'noexcept(false)'"
141 : "remove dynamic exception specification"),
142 source.isValid() ? source.getBegin() : decl->getLocation())
143 << (source.isValid() ? source : decl->getSourceRange());
144 return true;
147 private:
148 bool hasThrowsDocumentation(FunctionDecl const * decl) {
149 if (auto cmt = compiler.getASTContext().getCommentForDecl(
150 decl, &compiler.getPreprocessor()))
152 for (auto i = cmt->child_begin(); i != cmt->child_end(); ++i) {
153 if (auto bcc = dyn_cast<comments::BlockCommandComment>(*i)) {
154 if (compiler.getASTContext().getCommentCommandTraits()
155 .getCommandInfo(bcc->getCommandID())->IsThrowsCommand)
157 return true;
162 return false;
165 bool anyRedeclHasThrowsDocumentation(FunctionDecl const * decl) {
166 return std::any_of(
167 decl->redecls_begin(), decl->redecls_end(),
168 [this](FunctionDecl * d) { return hasThrowsDocumentation(d); });
169 // std::bind(
170 // &DynExcSpec::hasThrowsDocumentation, this,
171 // std::placeholders::_1));
175 loplugin::Plugin::Registration<DynExcSpec> X("dynexcspec", true);
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */