bump product version to 5.0.4.1
[LibreOffice.git] / compilerplugins / clang / stringconcat.cxx
blob43907e927cb30a3d69cafe09beb3eb468b567662
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 #include "plugin.hxx"
12 namespace {
14 class StringConcat:
15 public RecursiveASTVisitor<StringConcat>, public loplugin::Plugin
17 public:
18 explicit StringConcat(InstantiationData const & data): Plugin(data) {}
20 void run() override
21 { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
23 bool VisitCallExpr(CallExpr const * expr);
26 bool StringConcat::VisitCallExpr(CallExpr const * expr) {
27 if (ignoreLocation(expr)) {
28 return true;
30 FunctionDecl const * fdecl = expr->getDirectCallee();
31 if (fdecl == nullptr) {
32 return true;
34 OverloadedOperatorKind oo = fdecl->getOverloadedOperator();
35 if ((oo != OverloadedOperatorKind::OO_Plus
36 && oo != OverloadedOperatorKind::OO_LessLess)
37 || fdecl->getNumParams() != 2 || expr->getNumArgs() != 2
38 || !isa<StringLiteral>(expr->getArg(1)->IgnoreParenImpCasts()))
40 return true;
42 CallExpr const * left = dyn_cast<CallExpr>(
43 expr->getArg(0)->IgnoreParenImpCasts());
44 if (left == nullptr) {
45 return true;
47 FunctionDecl const * ldecl = left->getDirectCallee();
48 if (ldecl == nullptr) {
49 return true;
51 OverloadedOperatorKind loo = ldecl->getOverloadedOperator();
52 if ((loo != OverloadedOperatorKind::OO_Plus
53 && loo != OverloadedOperatorKind::OO_LessLess)
54 || ldecl->getNumParams() != 2 || left->getNumArgs() != 2
55 || !isa<StringLiteral>(left->getArg(1)->IgnoreParenImpCasts()))
57 return true;
59 StringRef name {
60 compiler.getSourceManager().getFilename(
61 compiler.getSourceManager().getSpellingLoc(expr->getLocStart())) };
62 if (name == SRCDIR "/sal/qa/rtl/strings/test_ostring_concat.cxx"
63 || name == SRCDIR "/sal/qa/rtl/strings/test_oustring_concat.cxx")
65 return true;
67 CXXOperatorCallExpr const * op = dyn_cast<CXXOperatorCallExpr>(expr);
68 report(
69 DiagnosticsEngine::Warning,
70 "replace '%0' between string literals with juxtaposition",
71 op == nullptr ? expr->getExprLoc() : op->getOperatorLoc())
72 << (oo == OverloadedOperatorKind::OO_Plus ? "+" : "<<")
73 << SourceRange(
74 left->getArg(1)->getLocStart(), expr->getArg(1)->getLocEnd());
75 return true;
78 loplugin::Plugin::Registration<StringConcat> X("stringconcat");
82 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */