blob: 23a7db9ebea278dfe5d1e5ade8f9cf1852904a5c (
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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* 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/.
*/
#include "plugin.hxx"
#include "check.hxx"
#include "compat.hxx"
#include <iostream>
namespace
{
class ConstStringField : public loplugin::FilteringPlugin<ConstStringField>
{
public:
explicit ConstStringField(loplugin::InstantiationData const& data)
: loplugin::FilteringPlugin<ConstStringField>(data)
{
}
void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
bool TraverseConstructorInitializer(CXXCtorInitializer* init);
};
bool ConstStringField::TraverseConstructorInitializer(CXXCtorInitializer* init)
{
if (!init->getSourceLocation().isValid() || ignoreLocation(init->getSourceLocation()))
return true;
if (!init->getMember())
return true;
auto tc = loplugin::TypeCheck(init->getMember()->getType());
if (!tc.Const().Class("OUString").Namespace("rtl").GlobalNamespace()
&& !tc.Const().Class("OString").Namespace("rtl").GlobalNamespace())
return true;
if (auto constructExpr = dyn_cast<CXXConstructExpr>(init->getInit()))
{
if (constructExpr->getNumArgs() >= 1 && isa<clang::StringLiteral>(constructExpr->getArg(0)))
{
report(DiagnosticsEngine::Warning, "string field can be static const",
init->getSourceLocation())
<< init->getSourceRange();
report(DiagnosticsEngine::Note, "field here", init->getMember()->getLocation())
<< init->getMember()->getSourceRange();
}
}
return RecursiveASTVisitor::TraverseConstructorInitializer(init);
}
loplugin::Plugin::Registration<ConstStringField> X("conststringfield", true);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|