bump product version to 5.0.4.1
[LibreOffice.git] / compilerplugins / clang / store / paintmethodconversion.cxx
blob47e410872b35a230f005aaeb791fbaebf4ea1a26
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 "clang/Lex/Lexer.h"
12 #include "compat.hxx"
13 #include "plugin.hxx"
14 #include <iostream>
15 #include <fstream>
17 /**
18 * Rewrites all Paint method on subclasses of vcl::Window to include RenderContext& as parameter.
20 * run as: make COMPILER_PLUGIN_TOOL=paintmethodconversion UPDATE_FILES=all FORCE_COMPILE_ALL=1
23 namespace
26 bool baseCheckNotWindowSubclass(const CXXRecordDecl* aBaseDefinition, void* /*pInput*/)
28 if (aBaseDefinition && aBaseDefinition->getQualifiedNameAsString() == "vcl::Window")
30 return false;
32 return true;
35 bool isDerivedFromWindow(const CXXRecordDecl* decl) {
36 if (!decl)
37 return false;
38 // skip vcl::Window
39 if (decl->getQualifiedNameAsString() == "vcl::Window")
40 return false;
41 if (!decl->forallBases(baseCheckNotWindowSubclass, nullptr, true))
42 return true;
44 return false;
47 class PaintMethodConversion: public RecursiveASTVisitor<PaintMethodConversion>, public loplugin::RewritePlugin
49 public:
50 explicit PaintMethodConversion(InstantiationData const& data):
51 RewritePlugin(data)
54 virtual void run() override
56 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
59 bool TraverseCXXMethodDecl(const CXXMethodDecl* methodDeclaration)
61 if (!rewriter)
62 return true;
64 if (methodDeclaration->getNameAsString() != "Paint")
65 return true;
67 if (!isDerivedFromWindow(methodDeclaration->getParent()))
69 return true;
72 unsigned int nNoOfParameters = methodDeclaration->getNumParams();
74 if (nNoOfParameters == 1) // we expect only one parameter Paint(Rect&)
76 const ParmVarDecl* parameterDecl = methodDeclaration->getParamDecl(0);
77 if (methodDeclaration->hasBody())
79 rewriter->InsertText(parameterDecl->getLocStart(), "vcl::RenderContext& /*rRenderContext*/, ", true, true);
81 else
83 rewriter->InsertText(parameterDecl->getLocStart(), "vcl::RenderContext& rRenderContext, ", true, true);
86 return true;
91 loplugin::Plugin::Registration<PaintMethodConversion> X("paintmethodconversion", true);
95 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */