LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / compilerplugins / clang / stringconcatliterals.cxx
blobf82114199de830255b927eaded63163664502f2d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #ifndef LO_CLANG_SHARED_PLUGINS
12 #include "plugin.hxx"
13 #include "check.hxx"
15 namespace {
17 Expr const * stripCtor(Expr const * expr) {
18 auto e1 = expr;
19 if (auto const e = dyn_cast<CXXFunctionalCastExpr>(e1)) {
20 e1 = e->getSubExpr()->IgnoreParenImpCasts();
22 if (auto const e = dyn_cast<CXXBindTemporaryExpr>(e1)) {
23 e1 = e->getSubExpr()->IgnoreParenImpCasts();
25 auto const e2 = dyn_cast<CXXConstructExpr>(e1);
26 if (e2 == nullptr) {
27 return expr;
29 auto qt = loplugin::DeclCheck(e2->getConstructor());
30 if (qt.MemberFunction().Class("OStringLiteral").Namespace("rtl").GlobalNamespace()
31 || qt.MemberFunction().Class("OUStringLiteral").Namespace("rtl").GlobalNamespace())
33 if (e2->getNumArgs() == 1) {
34 return e2->getArg(0)->IgnoreParenImpCasts();
36 return expr;
38 if (!((qt.MemberFunction().Class("OString").Namespace("rtl")
39 .GlobalNamespace())
40 || (qt.MemberFunction().Class("OUString").Namespace("rtl")
41 .GlobalNamespace())))
43 return expr;
45 if (e2->getNumArgs() != 2) {
46 return expr;
48 return e2->getArg(0)->IgnoreParenImpCasts();
51 class StringConcatLiterals:
52 public loplugin::FilteringPlugin<StringConcatLiterals>
54 public:
55 explicit StringConcatLiterals(loplugin::InstantiationData const & data):
56 FilteringPlugin(data) {}
58 void run() override
59 { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
61 bool VisitCallExpr(CallExpr const * expr);
63 private:
64 bool isStringLiteral(Expr const * expr);
67 bool StringConcatLiterals::VisitCallExpr(CallExpr const * expr) {
68 if (ignoreLocation(expr)) {
69 return true;
71 FunctionDecl const * fdecl = expr->getDirectCallee();
72 if (fdecl == nullptr) {
73 return true;
75 OverloadedOperatorKind oo = fdecl->getOverloadedOperator();
76 if ((oo != OverloadedOperatorKind::OO_Plus
77 && oo != OverloadedOperatorKind::OO_LessLess)
78 || fdecl->getNumParams() != 2 || expr->getNumArgs() != 2
79 || !isStringLiteral(expr->getArg(1)->IgnoreParenImpCasts()))
81 return true;
83 SourceLocation leftLoc;
84 auto const leftExpr = expr->getArg(0)->IgnoreParenImpCasts();
85 if (isStringLiteral(leftExpr)) {
86 leftLoc = compat::getBeginLoc(leftExpr);
87 } else {
88 CallExpr const * left = dyn_cast<CallExpr>(leftExpr);
89 if (left == nullptr) {
90 return true;
92 FunctionDecl const * ldecl = left->getDirectCallee();
93 if (ldecl == nullptr) {
94 return true;
96 OverloadedOperatorKind loo = ldecl->getOverloadedOperator();
97 if ((loo != OverloadedOperatorKind::OO_Plus
98 && loo != OverloadedOperatorKind::OO_LessLess)
99 || ldecl->getNumParams() != 2 || left->getNumArgs() != 2
100 || !isStringLiteral(left->getArg(1)->IgnoreParenImpCasts()))
102 return true;
104 leftLoc = compat::getBeginLoc(left->getArg(1));
107 // We add an extra " " in the TOOLS_WARN_EXCEPTION macro, which triggers this plugin
108 if (loplugin::isSamePathname(
109 getFilenameOfLocation(
110 compiler.getSourceManager().getSpellingLoc(
111 compiler.getSourceManager().getImmediateMacroCallerLoc(
112 compiler.getSourceManager().getImmediateMacroCallerLoc(
113 compiler.getSourceManager().getImmediateMacroCallerLoc(
114 compat::getBeginLoc(expr)))))),
115 SRCDIR "/include/tools/diagnose_ex.h"))
116 return true;
118 StringRef name {
119 getFilenameOfLocation(
120 compiler.getSourceManager().getSpellingLoc(compat::getBeginLoc(expr))) };
121 if (loplugin::isSamePathname(
122 name, SRCDIR "/sal/qa/rtl/oustringbuffer/test_oustringbuffer_assign.cxx")
123 || loplugin::isSamePathname(
124 name, SRCDIR "/sal/qa/rtl/strings/test_ostring_concat.cxx")
125 || loplugin::isSamePathname(
126 name, SRCDIR "/sal/qa/rtl/strings/test_oustring_concat.cxx"))
128 return true;
130 CXXOperatorCallExpr const * op = dyn_cast<CXXOperatorCallExpr>(expr);
131 report(
132 DiagnosticsEngine::Warning,
133 "replace '%0' between string literals with juxtaposition",
134 op == nullptr ? expr->getExprLoc() : op->getOperatorLoc())
135 << (oo == OverloadedOperatorKind::OO_Plus ? "+" : "<<")
136 << SourceRange(leftLoc, compat::getEndLoc(expr->getArg(1)));
137 return true;
140 bool StringConcatLiterals::isStringLiteral(Expr const * expr) {
141 expr = stripCtor(expr);
142 if (!isa<clang::StringLiteral>(expr)) {
143 return false;
145 return true;
148 loplugin::Plugin::Registration<StringConcatLiterals> stringconcatliterals("stringconcatliterals");
150 } // namespace
152 #endif // LO_CLANG_SHARED_PLUGINS
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */