Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / colorcheck.cxx
blob69bcaa0cd9cc445bf2812fcb771d9a185ef52891
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 */
9 #ifndef LO_CLANG_SHARED_PLUGINS
11 #include <memory>
12 #include <cassert>
13 #include <string>
14 #include <iostream>
15 #include <fstream>
16 #include <set>
18 #include "config_clang.h"
20 #include "check.hxx"
21 #include "compat.hxx"
22 #include "plugin.hxx"
24 /**
27 namespace
29 class ColorCheck : public loplugin::FilteringPlugin<ColorCheck>
31 public:
32 explicit ColorCheck(loplugin::InstantiationData const& data)
33 : FilteringPlugin(data)
37 virtual void run() override
39 if (preRun())
40 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
43 bool VisitCXXConstructExpr(const CXXConstructExpr*);
46 bool ColorCheck::VisitCXXConstructExpr(const CXXConstructExpr* constructExpr)
48 if (ignoreLocation(constructExpr))
49 return true;
51 if (constructExpr->getNumArgs() != 1)
52 return true;
54 auto tc = loplugin::TypeCheck(constructExpr->getType());
55 if (!tc.Class("Color").GlobalNamespace())
56 return true;
58 StringRef aFileName = getFilenameOfLocation(
59 compiler.getSourceManager().getSpellingLoc(constructExpr->getBeginLoc()));
60 if (loplugin::isSamePathname(aFileName, SRCDIR "/include/tools/color.hxx"))
61 return true;
63 const CXXConstructorDecl* constructorDecl = constructExpr->getConstructor();
64 constructorDecl = constructorDecl->getCanonicalDecl();
66 if (!loplugin::TypeCheck(constructorDecl->getParamDecl(0)->getType())
67 .Typedef("sal_uInt32")
68 .GlobalNamespace())
69 return true;
71 auto arg0 = constructExpr->getArg(0);
72 if (arg0->isCXX11ConstantExpr(compiler.getASTContext()))
74 if (!arg0->isValueDependent())
76 compat::optional<llvm::APSInt> xVal
77 = arg0->getIntegerConstantExpr(compiler.getASTContext());
78 if (xVal && *xVal > 0xffffff)
79 report(DiagnosticsEngine::Warning,
80 "Rather use the ColorTransparency or ColorAlpha version of this constructor",
81 arg0->getExprLoc());
82 return true;
85 report(DiagnosticsEngine::Warning,
86 "Rather use the ColorTransparency or ColorAlpha version of this constructor",
87 arg0->getExprLoc());
89 return true;
92 loplugin::Plugin::Registration<ColorCheck> colorcheck("colorcheck");
94 } // namespace
96 #endif // LO_CLANG_SHARED_PLUGINS
98 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */