bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / conststringvar.cxx
bloba323b1cf0480b5adb548e62042566b8b1cb059ec
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 <set>
11 #include <stack>
13 #include "check.hxx"
14 #include "plugin.hxx"
16 // Find non-const vars of 'char const *' type initialized with a const expr,
17 // that could likely be const (and will then probably trigger further
18 // loplugin:stringconstant findings).
20 namespace {
22 // It looks like Clang wrongly implements DR 4
23 // (<http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#4>) and treats
24 // a variable declared in an 'extern "..." {...}'-style linkage-specification as
25 // if it contained the 'extern' specifier:
26 bool hasExternalLinkage(VarDecl const * decl) {
27 if (decl->getLinkageAndVisibility().getLinkage() != ExternalLinkage) {
28 return false;
30 for (auto ctx = decl->getLexicalDeclContext();
31 ctx->getDeclKind() != Decl::TranslationUnit;
32 ctx = ctx->getLexicalParent())
34 if (auto ls = dyn_cast<LinkageSpecDecl>(ctx)) {
35 if (!ls->hasBraces()) {
36 return true;
38 if (auto prev = decl->getPreviousDecl()) {
39 return hasExternalLinkage(prev);
41 return !decl->isInAnonymousNamespace();
44 return true;
47 class ConstStringVar:
48 public loplugin::FilteringPlugin<ConstStringVar>
50 public:
51 explicit ConstStringVar(loplugin::InstantiationData const & data):
52 FilteringPlugin(data) {}
54 void run() override {
55 if (compiler.getLangOpts().CPlusPlus) {
56 // clang::Expr::isCXX11ConstantExpr only works for C++
57 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
58 for (auto v: vars_) {
59 report(
60 DiagnosticsEngine::Warning,
61 "variable is only used as rvalue, should be const",
62 v->getLocation())
63 << v->getSourceRange();
68 bool TraverseImplicitCastExpr(ImplicitCastExpr * expr) {
69 bool match;
70 switch (expr->getCastKind()) {
71 case CK_NoOp:
72 // OString CharPtrDetector ctor:
73 match = bool(
74 loplugin::TypeCheck(expr->getType()).Const().Pointer().Const()
75 .Char());
76 break;
77 case CK_LValueToRValue:
78 match = true;
79 break;
80 default:
81 match = false;
82 break;
84 bool pushed = false;
85 if (match) {
86 if (auto dr = dyn_cast<DeclRefExpr>(
87 expr->getSubExpr()->IgnoreParenImpCasts()))
89 if (auto vd = dyn_cast<VarDecl>(dr->getDecl())) {
90 if (vars_.find(vd->getCanonicalDecl()) != vars_.end()) {
91 casted_.push(dr);
92 pushed = true;
97 bool b = RecursiveASTVisitor::TraverseImplicitCastExpr(expr);
98 if (pushed) {
99 casted_.pop();
101 return b;
104 bool VisitVarDecl(VarDecl const * decl) {
105 if (ignoreLocation(decl)) {
106 return true;
108 if (decl != decl->getCanonicalDecl()) {
109 return true;
111 if (isa<ParmVarDecl>(decl) || hasExternalLinkage(decl)) {
112 return true;
114 if (!loplugin::TypeCheck(decl->getType()).NonConstVolatile().Pointer()
115 .Const().Char())
117 return true;
119 auto init = decl->getAnyInitializer();
120 if (init == nullptr) {
121 return true;
123 if (init->isInstantiationDependent()) {
124 // avoid problems with isCXX11ConstantExpr in template code
125 return true;
127 APValue v;
128 if (!init->isCXX11ConstantExpr(compiler.getASTContext(), &v)) {
129 return true;
131 vars_.insert(decl);
132 return true;
135 bool VisitDeclRefExpr(DeclRefExpr const * expr) {
136 if (!casted_.empty() && expr == casted_.top()) {
137 return true;
139 auto vd = dyn_cast<VarDecl>(expr->getDecl());
140 if (vd == nullptr) {
141 return true;
143 vars_.erase(vd->getCanonicalDecl());
144 return true;
147 private:
148 std::set<VarDecl const *> vars_;
149 std::stack<DeclRefExpr const *> casted_;
152 loplugin::Plugin::Registration<ConstStringVar> X("conststringvar");
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */