Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / noexcept.cxx
blobde956839e82731df7ac633a3b26088543d6fc279
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 "plugin.hxx"
12 namespace
14 class Noexcept : public loplugin::FilteringRewritePlugin<Noexcept>
16 public:
17 explicit Noexcept(loplugin::InstantiationData const& data)
18 : FilteringRewritePlugin(data)
22 void run() override
24 // Don't execute for < C++11, so we don't accidentally rewrite the legacy definitions of
25 // SAL_THROW_EXTERN_C and SAL_NOEXCEPT in include/sal/types.h e.g. when building
26 // CppunitTest_odk_checkapi which explicitly uses gb_CXX03FLAGS:
27 if (compiler.getLangOpts().CPlusPlus11)
29 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
33 bool VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc tloc)
35 if (ignoreLocation(tloc))
37 return true;
39 if (tloc.getTypePtr()->getExceptionSpecType() != EST_DynamicNone)
41 return true;
43 auto const r = tloc.getExceptionSpecRange();
44 auto r2 = r;
45 auto l1 = r.getBegin();
46 while (compiler.getSourceManager().isMacroArgExpansion(l1))
48 l1 = compiler.getSourceManager().getImmediateMacroCallerLoc(l1);
50 if (compiler.getSourceManager().isMacroBodyExpansion(l1))
52 auto l2 = r.getEnd();
53 while (compiler.getSourceManager().isMacroArgExpansion(l2))
55 l2 = compiler.getSourceManager().getImmediateMacroCallerLoc(l2);
57 if (compiler.getSourceManager().isMacroBodyExpansion(l2))
59 //TODO: check l1, l2 are in same macro body expansion
60 auto const spl = compiler.getSourceManager().getSpellingLoc(l1);
61 if (ignoreLocation(spl))
63 return true;
65 r2 = { spl, compiler.getSourceManager().getSpellingLoc(l2) };
68 auto const repl = isInUnoIncludeFile(r.getBegin()) ? "SAL_NOEXCEPT" : "noexcept";
69 if (rewriter != nullptr && replaceText(r2, repl))
71 return true;
73 report(DiagnosticsEngine::Warning,
74 "Replace legacy dynamic 'throw ()' exception specification with '%0'", r.getBegin())
75 << repl << r;
76 return true;
80 loplugin::Plugin::Registration<Noexcept> X("noexcept", true);
83 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */