Update git submodules
[LibreOffice.git] / compilerplugins / clang / store / paintmethodconversion.cxx
blob7a394ae253826e7b4084132697355dba280d8ebe
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 "plugin.hxx"
13 #include <iostream>
14 #include <fstream>
16 /**
17 * Rewrites all Paint method on subclasses of vcl::Window to include RenderContext& as parameter.
19 * run as: make COMPILER_PLUGIN_TOOL=paintmethodconversion UPDATE_FILES=all FORCE_COMPILE=all
22 namespace
25 bool baseCheckNotWindowSubclass(const CXXRecordDecl* aBaseDefinition, void* /*pInput*/)
27 if (aBaseDefinition && aBaseDefinition->getQualifiedNameAsString() == "vcl::Window")
29 return false;
31 return true;
34 bool isDerivedFromWindow(const CXXRecordDecl* decl) {
35 if (!decl)
36 return false;
37 // skip vcl::Window
38 if (decl->getQualifiedNameAsString() == "vcl::Window")
39 return false;
40 if (!decl->forallBases(baseCheckNotWindowSubclass, nullptr, true))
41 return true;
43 return false;
46 class PaintMethodConversion: public loplugin::FilteringRewritePlugin<PaintMethodConversion>
48 public:
49 explicit PaintMethodConversion(InstantiationData const& data):
50 FilteringRewritePlugin(data)
53 virtual void run() override
55 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
58 bool TraverseCXXMethodDecl(const CXXMethodDecl* methodDeclaration)
60 if (!rewriter)
61 return true;
63 if (methodDeclaration->getNameAsString() != "Paint")
64 return true;
66 if (!isDerivedFromWindow(methodDeclaration->getParent()))
68 return true;
71 unsigned int nNoOfParameters = methodDeclaration->getNumParams();
73 if (nNoOfParameters == 1) // we expect only one parameter Paint(Rect&)
75 const ParmVarDecl* parameterDecl = methodDeclaration->getParamDecl(0);
76 if (methodDeclaration->hasBody())
78 rewriter->InsertText(parameterDecl->getLocStart(), "vcl::RenderContext& /*rRenderContext*/, ", true, true);
80 else
82 rewriter->InsertText(parameterDecl->getLocStart(), "vcl::RenderContext& rRenderContext, ", true, true);
85 return true;
90 loplugin::Plugin::Registration<PaintMethodConversion> X("paintmethodconversion", true);
94 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */