summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/dynexcspec.cxx
blob: af733ec4f10fa55fab9342a5c2cf9c8295b7d464 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
#ifndef LO_CLANG_SHARED_PLUGINS

#include <algorithm>
#include <functional>

#include "clang/AST/Comment.h"

#include "plugin.hxx"

// Remove dynamic exception specifications.  See the mail thread starting at
// <https://lists.freedesktop.org/archives/libreoffice/2017-January/076665.html>
// "Dynamic Exception Specifications" for details.

namespace {

bool isOverriding(FunctionDecl const * decl) {
    if (decl->hasAttr<OverrideAttr>()) {
        return true;
    }
    auto m = dyn_cast<CXXMethodDecl>(decl);
    return m != nullptr
        && m->begin_overridden_methods() != m->end_overridden_methods();
}

bool isDtorOrDealloc(FunctionDecl const * decl) {
    if (isa<CXXDestructorDecl>(decl)) {
        return true;
    }
    switch (decl->getOverloadedOperator()) {
    case OO_Delete:
    case OO_Array_Delete:
        return true;
    default:
        return false;
    }
}

class DynExcSpec:
    public loplugin::FilteringRewritePlugin<DynExcSpec>
{
public:
    explicit DynExcSpec(loplugin::InstantiationData const & data):
        FilteringRewritePlugin(data) {}

    bool preRun() override {
        return compiler.getLangOpts().CPlusPlus;
    }

    void run() override {
        if (preRun()) {
            TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
        }
    }

    bool VisitFunctionDecl(FunctionDecl const * decl) {
        if (ignoreLocation(decl)) {
            return true;
        }
        auto proto = decl->getType()->getAs<FunctionProtoType>();
        if (proto == nullptr || proto->getExceptionSpecType() != EST_Dynamic) {
            return true;
        }
        if (decl->isCanonicalDecl() && !isOverriding(decl)
            && !anyRedeclHasThrowsDocumentation(decl))
        {
            report(
                DiagnosticsEngine::Warning,
                ("function declaration has dynamic exception specification but"
                 " no corresponding documentation comment"),
                decl->getLocation())
                << decl->getSourceRange();
        }
        if (rewriter != nullptr) {
            if (!(decl->isDefined() || decl->isPure())) {
                return true;
            }
            if (auto m = dyn_cast<CXXMethodDecl>(decl)) {
                for (auto i = m->begin_overridden_methods();
                     i != m->end_overridden_methods(); ++i)
                {
                    auto proto2 = (*i)->getType()->getAs<FunctionProtoType>();
                    assert(proto2 != nullptr);
                    if (proto2->getExceptionSpecType() == EST_Dynamic) {
                        return true;
                    }
                }
            }
        }
        bool dtorOrDealloc = isDtorOrDealloc(decl);
        auto const source = decl->getExceptionSpecSourceRange();
        if (rewriter != nullptr && source.isValid()) {
            if (dtorOrDealloc) {
                if (replaceText(source, "noexcept(false)")) {
                    return true;
                }
            } else {
                auto beg = source.getBegin();
                if (beg.isFileID()) {
                    for (;;) {
                        auto prev = Lexer::GetBeginningOfToken(
                            beg.getLocWithOffset(-1),
                            compiler.getSourceManager(),
                            compiler.getLangOpts());
                        auto n = Lexer::MeasureTokenLength(
                            prev, compiler.getSourceManager(),
                            compiler.getLangOpts());
                        auto s = StringRef(
                            compiler.getSourceManager().getCharacterData(prev),
                            n);
                        while (s.startswith("\\\n")) {
                            s = s.drop_front(2);
                            while (!s.empty()
                                   && (s.front() == ' ' || s.front() == '\t'
                                       || s.front() == '\n' || s.front() == '\v'
                                       || s.front() == '\f'))
                            {
                                s = s.drop_front(1);
                            }
                        }
                        if (!s.empty() && s != "\\") {
                            if (s.startswith("//")) {
                                beg = source.getBegin();
                            }
                            break;
                        }
                        beg = prev;
                    }
                }
                if (removeText(SourceRange(beg, source.getEnd()))) {
                    return true;
                }
            }
        }
        report(
            DiagnosticsEngine::Warning,
            (dtorOrDealloc
             ? "replace dynamic exception specification with 'noexcept(false)'"
             : "remove dynamic exception specification"),
            source.isValid() ? source.getBegin() : decl->getLocation())
            << (source.isValid() ? source : decl->getSourceRange());
        return true;
    }

private:
    bool hasThrowsDocumentation(FunctionDecl const * decl) {
        if (auto cmt = compiler.getASTContext().getCommentForDecl(
            decl, &compiler.getPreprocessor()))
        {
            for (auto i = cmt->child_begin(); i != cmt->child_end(); ++i) {
                if (auto bcc = dyn_cast<comments::BlockCommandComment>(*i)) {
                    if (compiler.getASTContext().getCommentCommandTraits()
                        .getCommandInfo(bcc->getCommandID())->IsThrowsCommand)
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    bool anyRedeclHasThrowsDocumentation(FunctionDecl const * decl) {
        return std::any_of(
            decl->redecls_begin(), decl->redecls_end(),
            [this](FunctionDecl * d) { return hasThrowsDocumentation(d); });
            // std::bind(
            //     &DynExcSpec::hasThrowsDocumentation, this,
            //     std::placeholders::_1));
    }
};

loplugin::Plugin::Registration<DynExcSpec> dynexcspec("dynexcspec");

} // namespace

#endif // LO_CLANG_SHARED_PLUGINS

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */