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 loplugin::FilteringRewritePlugin
<DynExcSpec
>
49 explicit DynExcSpec(loplugin::InstantiationData
const & data
):
50 FilteringRewritePlugin(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
);
93 auto const source
= decl
->getExceptionSpecSourceRange();
94 if (rewriter
!= nullptr && source
.isValid()) {
96 if (replaceText(source
, "noexcept(false)")) {
100 auto beg
= source
.getBegin();
101 if (beg
.isFileID()) {
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());
111 compiler
.getSourceManager().getCharacterData(prev
),
113 while (s
.startswith("\\\n")) {
116 && (s
.front() == ' ' || s
.front() == '\t'
117 || s
.front() == '\n' || s
.front() == '\v'
118 || s
.front() == '\f'))
123 if (!s
.empty() && s
!= "\\") {
124 if (s
.startswith("//")) {
125 beg
= source
.getBegin();
132 if (removeText(SourceRange(beg
, source
.getEnd()))) {
138 DiagnosticsEngine::Warning
,
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());
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
)
165 bool anyRedeclHasThrowsDocumentation(FunctionDecl
const * decl
) {
167 decl
->redecls_begin(), decl
->redecls_end(),
168 [this](FunctionDecl
* d
) { return hasThrowsDocumentation(d
); });
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: */