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/.
13 #include "clang/AST/Comment.h"
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.
23 bool isOverriding(FunctionDecl
const * decl
) {
24 if (decl
->hasAttr
<OverrideAttr
>()) {
27 auto m
= dyn_cast
<CXXMethodDecl
>(decl
);
29 && m
->begin_overridden_methods() != m
->end_overridden_methods();
32 bool isDtorOrDealloc(FunctionDecl
const * decl
) {
33 if (isa
<CXXDestructorDecl
>(decl
)) {
36 switch (decl
->getOverloadedOperator()) {
46 public RecursiveASTVisitor
<DynExcSpec
>, public loplugin::RewritePlugin
49 explicit DynExcSpec(loplugin::InstantiationData
const & data
):
50 RewritePlugin(data
) {}
53 if (compiler
.getLangOpts().CPlusPlus
) {
54 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
58 bool VisitFunctionDecl(FunctionDecl
const * decl
) {
59 if (ignoreLocation(decl
)) {
62 auto proto
= decl
->getType()->getAs
<FunctionProtoType
>();
63 if (proto
== nullptr || proto
->getExceptionSpecType() != EST_Dynamic
) {
66 if (decl
->isCanonicalDecl() && !isOverriding(decl
)
67 && !anyRedeclHasThrowsDocumentation(decl
))
70 DiagnosticsEngine::Warning
,
71 ("function declaration has dynamic exception specification but"
72 " no corresponding documentation comment"),
74 << decl
->getSourceRange();
76 if (rewriter
!= nullptr) {
77 if (!(decl
->isDefined() || decl
->isPure())) {
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
) {
92 bool dtorOrDealloc
= isDtorOrDealloc(decl
);
94 #if CLANG_VERSION >= 40000
95 source
= decl
->getExceptionSpecSourceRange();
97 if (rewriter
!= nullptr && source
.isValid()) {
99 if (replaceText(source
, "noexcept(false)")) {
103 auto beg
= source
.getBegin();
104 if (beg
.isFileID()) {
106 auto prev
= Lexer::GetBeginningOfToken(
107 beg
.getLocWithOffset(-1),
108 compiler
.getSourceManager(),
109 compiler
.getLangOpts());
110 auto n
= Lexer::MeasureTokenLength(
111 prev
, compiler
.getSourceManager(),
112 compiler
.getLangOpts());
114 compiler
.getSourceManager().getCharacterData(prev
),
116 while (s
.startswith("\\\n")) {
119 && (s
.front() == ' ' || s
.front() == '\t'
120 || s
.front() == '\n' || s
.front() == '\v'
121 || s
.front() == '\f'))
126 if (!s
.empty() && s
!= "\\") {
127 if (s
.startswith("//")) {
128 beg
= source
.getBegin();
135 if (removeText(SourceRange(beg
, source
.getEnd()))) {
141 DiagnosticsEngine::Warning
,
143 ? "replace dynamic exception specification with 'noexcept(false)'"
144 : "remove dynamic exception specification"),
145 source
.isValid() ? source
.getBegin() : decl
->getLocation())
146 << (source
.isValid() ? source
: decl
->getSourceRange());
151 bool hasThrowsDocumentation(FunctionDecl
const * decl
) {
152 if (auto cmt
= compiler
.getASTContext().getCommentForDecl(
153 decl
, &compiler
.getPreprocessor()))
155 for (auto i
= cmt
->child_begin(); i
!= cmt
->child_end(); ++i
) {
156 if (auto bcc
= dyn_cast
<comments::BlockCommandComment
>(*i
)) {
157 if (compiler
.getASTContext().getCommentCommandTraits()
158 .getCommandInfo(bcc
->getCommandID())->IsThrowsCommand
)
168 bool anyRedeclHasThrowsDocumentation(FunctionDecl
const * decl
) {
170 decl
->redecls_begin(), decl
->redecls_end(),
171 [this](FunctionDecl
* d
) { return hasThrowsDocumentation(d
); });
173 // &DynExcSpec::hasThrowsDocumentation, this,
174 // std::placeholders::_1));
178 loplugin::Plugin::Registration
<DynExcSpec
> X("dynexcspec", true);
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */