Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / oslendian.cxx
blob75b1dcfdde9f70f844d9dd2b2b2f20d5e3ebb179
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 <cassert>
11 #include <memory>
13 #include "plugin.hxx"
15 namespace {
17 class OslEndian: public loplugin::Plugin, public PPCallbacks {
18 public:
19 explicit OslEndian(loplugin::InstantiationData const & data): Plugin(data) {
20 compiler.getPreprocessor().addPPCallbacks(std::unique_ptr<PPCallbacks>(this));
23 enum { isPPCallback = true };
25 private:
26 void run() override {}
28 void MacroDefined(Token const & MacroNameTok, MacroDirective const *)
29 override
31 auto id = MacroNameTok.getIdentifierInfo()->getName();
32 if (id == "OSL_BIGENDIAN") {
33 if (definedLit_.isValid()) {
34 report(
35 DiagnosticsEngine::Warning,
36 "macro %0 defined in addition to 'OSL_LITENDIAN'",
37 MacroNameTok.getLocation())
38 << MacroNameTok.getIdentifierInfo();
39 report(
40 DiagnosticsEngine::Note,
41 "conflicting macro definition is here", definedLit_);
43 definedBig_ = MacroNameTok.getLocation();
44 assert(definedBig_.isValid());
45 } else if (id == "OSL_LITENDIAN") {
46 if (definedBig_.isValid()) {
47 report(
48 DiagnosticsEngine::Warning,
49 "macro %0 defined in addition to 'OSL_BIGENDIAN'",
50 MacroNameTok.getLocation())
51 << MacroNameTok.getIdentifierInfo();
52 report(
53 DiagnosticsEngine::Note,
54 "conflicting macro definition is here", definedBig_);
56 definedLit_ = MacroNameTok.getLocation();
57 assert(definedLit_.isValid());
61 void MacroUndefined(
62 Token const & MacroNameTok, MacroDefinition const &
63 #if CLANG_VERSION >= 50000
64 , MacroDirective const *
65 #endif
66 ) override
68 auto id = MacroNameTok.getIdentifierInfo()->getName();
69 if (id == "OSL_BIGENDIAN" || id == "OSL_LITENDIAN") {
70 report(
71 DiagnosticsEngine::Warning, "macro %0 undefinition",
72 MacroNameTok.getLocation())
73 << MacroNameTok.getIdentifierInfo();
77 void Defined(
78 Token const & MacroNameTok, MacroDefinition const &, SourceRange)
79 override
81 check(MacroNameTok);
84 void Ifdef(
85 SourceLocation, Token const & MacroNameTok,
86 MacroDefinition const &) override
88 check(MacroNameTok);
91 void Ifndef(
92 SourceLocation, Token const & MacroNameTok,
93 MacroDefinition const &) override
95 check(MacroNameTok);
98 void check(Token const & macro) const {
99 auto id = macro.getIdentifierInfo()->getName();
100 if ((id == "OSL_BIGENDIAN" || id == "OSL_LITENDIAN")
101 && definedBig_.isInvalid() && definedLit_.isInvalid())
103 report(
104 DiagnosticsEngine::Warning,
105 "definition of macro %0 checked but 'osl/endian.h' is not"
106 " included",
107 macro.getLocation())
108 << macro.getIdentifierInfo();
112 SourceLocation definedBig_;
113 SourceLocation definedLit_;
116 loplugin::Plugin::Registration<OslEndian> X("oslendian");
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */