GPU-Calc: remove Alloc_Host_Ptr for clmem of NAN vector
[LibreOffice.git] / compilerplugins / clang / literalalternative.cxx
blob4d2711c3d64c476f0246e80a3bd593ef8c662880
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 <string>
12 #include "plugin.hxx"
14 // Find those calls of rtl::OUString::equalsIgnoreAsciiCaseAscii and
15 // rtl::OUString::equalsIgnoreAsciiCaseAsciiL that could be simplified to call
16 // rtl::OUString::equalsIgnoreAsciiCase instead:
18 namespace {
20 class LiteralAlternative:
21 public RecursiveASTVisitor<LiteralAlternative>, public loplugin::Plugin
23 public:
24 explicit LiteralAlternative(CompilerInstance & compiler): Plugin(compiler) {}
26 virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
28 bool VisitCallExpr(const CallExpr * expr);
31 bool LiteralAlternative::VisitCallExpr(const CallExpr * expr) {
32 if (ignoreLocation(expr)) {
33 return true;
35 FunctionDecl const * fdecl = expr->getDirectCallee();
36 if (fdecl == nullptr) {
37 return true;
39 std::string qname { fdecl->getQualifiedNameAsString() };
40 if (qname == "rtl::OUString::equalsIgnoreAsciiCaseAscii"
41 && fdecl->getNumParams() == 1 && expr->getNumArgs() == 1)
43 // equalsIgnoreAsciiCaseAscii("foo") -> equalsIngoreAsciiCase("foo"):
44 StringLiteral const * lit
45 = dyn_cast<StringLiteral>(expr->getArg(0)->IgnoreParenImpCasts());
46 if (lit != nullptr) {
47 report(
48 DiagnosticsEngine::Warning,
49 ("rewrite call of rtl::OUString::equalsIgnoreAsciiCaseAscii"
50 " with string literal argument as call of"
51 " rtl::OUString::equalsIgnoreAsciiCase"),
52 expr->getExprLoc());
53 //TODO: a better loc (the "equalsIgnoreAsciiCaseAscii" part)?
55 return true;
57 if (qname == "rtl::OUString::equalsIgnoreAsciiCaseAsciiL"
58 && fdecl->getNumParams() == 2 && expr->getNumArgs() == 2)
60 // equalsIgnoreAsciiCaseAsciiL("foo", 3) -> equalsIngoreAsciiCase("foo")
61 // especially also for
62 // equalsIgnoreAsciiCaseAsciiL(RTL_CONSTASCII_STRINGPARAM("foo")), where
63 // RTL_CONSTASCII_STRINGPARAM expands to complicated expressions
64 // involving (&(X)[0] sub-expressions (and it might or might not be
65 // better to handle that at the level of non-expanded macros instead,
66 // but I have not found out how to do that yet anyway):
67 APSInt res;
68 if (expr->getArg(1)->isIntegerConstantExpr(res, compiler.getASTContext())) {
69 Expr const * arg0 = expr->getArg(0)->IgnoreParenImpCasts();
70 StringLiteral const * lit = dyn_cast<StringLiteral>(arg0);
71 bool match = false;
72 if (lit != nullptr) {
73 match = res == lit->getLength();
74 } else {
75 UnaryOperator const * op = dyn_cast<UnaryOperator>(arg0);
76 if (op != nullptr && op->getOpcode() == UO_AddrOf) {
77 ArraySubscriptExpr const * subs
78 = dyn_cast<ArraySubscriptExpr>(
79 op->getSubExpr()->IgnoreParenImpCasts());
80 if (subs != nullptr) {
81 lit = dyn_cast<StringLiteral>(
82 subs->getBase()->IgnoreParenImpCasts());
83 match = lit != nullptr
84 && subs->getIdx()->isIntegerConstantExpr(
85 res, compiler.getASTContext())
86 && res == 0;
90 if (match) {
91 report(
92 DiagnosticsEngine::Warning,
93 ("rewrite call of"
94 " rtl::OUString::equalsIgnoreAsciiCaseAsciiL with string"
95 " literal and matching length arguments as call of"
96 " rtl::OUString::equalsIgnoreAsciiCase"),
97 expr->getExprLoc());
98 //TODO: a better loc (the "equalsIgnoreAsciiCaseAsciiL"
99 // part)?
102 return true;
104 return true;
107 loplugin::Plugin::Registration< LiteralAlternative > X("literalalternative");
111 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */