1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
9 #ifndef LO_CLANG_SHARED_PLUGINS
14 #include "clang/AST/Comment.h"
18 // Remove dynamic exception specifications. See the mail thread starting at
19 // <https://lists.freedesktop.org/archives/libreoffice/2017-January/076665.html>
20 // "Dynamic Exception Specifications" for details.
24 bool isOverriding(FunctionDecl
const * decl
) {
25 if (decl
->hasAttr
<OverrideAttr
>()) {
28 auto m
= dyn_cast
<CXXMethodDecl
>(decl
);
30 && m
->begin_overridden_methods() != m
->end_overridden_methods();
33 bool isDtorOrDealloc(FunctionDecl
const * decl
) {
34 if (isa
<CXXDestructorDecl
>(decl
)) {
37 switch (decl
->getOverloadedOperator()) {
47 public loplugin::FilteringRewritePlugin
<DynExcSpec
>
50 explicit DynExcSpec(loplugin::InstantiationData
const & data
):
51 FilteringRewritePlugin(data
) {}
53 bool preRun() override
{
54 return compiler
.getLangOpts().CPlusPlus
;
59 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
63 bool VisitFunctionDecl(FunctionDecl
const * decl
) {
64 if (ignoreLocation(decl
)) {
67 auto proto
= decl
->getType()->getAs
<FunctionProtoType
>();
68 if (proto
== nullptr || proto
->getExceptionSpecType() != EST_Dynamic
) {
71 if (decl
->isCanonicalDecl() && !isOverriding(decl
)
72 && !anyRedeclHasThrowsDocumentation(decl
))
75 DiagnosticsEngine::Warning
,
76 ("function declaration has dynamic exception specification but"
77 " no corresponding documentation comment"),
79 << decl
->getSourceRange();
81 if (rewriter
!= nullptr) {
82 if (!(decl
->isDefined() || decl
->isPure())) {
85 if (auto m
= dyn_cast
<CXXMethodDecl
>(decl
)) {
86 for (auto i
= m
->begin_overridden_methods();
87 i
!= m
->end_overridden_methods(); ++i
)
89 auto proto2
= (*i
)->getType()->getAs
<FunctionProtoType
>();
90 assert(proto2
!= nullptr);
91 if (proto2
->getExceptionSpecType() == EST_Dynamic
) {
97 bool dtorOrDealloc
= isDtorOrDealloc(decl
);
98 auto const source
= decl
->getExceptionSpecSourceRange();
99 if (rewriter
!= nullptr && source
.isValid()) {
101 if (replaceText(source
, "noexcept(false)")) {
105 auto beg
= source
.getBegin();
106 if (beg
.isFileID()) {
108 auto prev
= Lexer::GetBeginningOfToken(
109 beg
.getLocWithOffset(-1),
110 compiler
.getSourceManager(),
111 compiler
.getLangOpts());
112 auto n
= Lexer::MeasureTokenLength(
113 prev
, compiler
.getSourceManager(),
114 compiler
.getLangOpts());
116 compiler
.getSourceManager().getCharacterData(prev
),
118 while (s
.startswith("\\\n")) {
121 && (s
.front() == ' ' || s
.front() == '\t'
122 || s
.front() == '\n' || s
.front() == '\v'
123 || s
.front() == '\f'))
128 if (!s
.empty() && s
!= "\\") {
129 if (s
.startswith("//")) {
130 beg
= source
.getBegin();
137 if (removeText(SourceRange(beg
, source
.getEnd()))) {
143 DiagnosticsEngine::Warning
,
145 ? "replace dynamic exception specification with 'noexcept(false)'"
146 : "remove dynamic exception specification"),
147 source
.isValid() ? source
.getBegin() : decl
->getLocation())
148 << (source
.isValid() ? source
: decl
->getSourceRange());
153 bool hasThrowsDocumentation(FunctionDecl
const * decl
) {
154 if (auto cmt
= compiler
.getASTContext().getCommentForDecl(
155 decl
, &compiler
.getPreprocessor()))
157 for (auto i
= cmt
->child_begin(); i
!= cmt
->child_end(); ++i
) {
158 if (auto bcc
= dyn_cast
<comments::BlockCommandComment
>(*i
)) {
159 if (compiler
.getASTContext().getCommentCommandTraits()
160 .getCommandInfo(bcc
->getCommandID())->IsThrowsCommand
)
170 bool anyRedeclHasThrowsDocumentation(FunctionDecl
const * decl
) {
172 decl
->redecls_begin(), decl
->redecls_end(),
173 [this](FunctionDecl
* d
) { return hasThrowsDocumentation(d
); });
175 // &DynExcSpec::hasThrowsDocumentation, this,
176 // std::placeholders::_1));
180 loplugin::Plugin::Registration
<DynExcSpec
> dynexcspec("dynexcspec");
184 #endif // LO_CLANG_SHARED_PLUGINS
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */