bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / oslendian.cxx
blob13296330baf67acda78c2e3ba1e355a0871810a6
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 virtual void FileChanged(SourceLocation, FileChangeReason, SrcMgr::CharacteristicKind, FileID) override {
29 if(!startChecked) {
30 // With precompiled headers MacroDefined() would not be called, so check already at the very
31 // start whether the macros exist.
32 startChecked = true;
33 if(const MacroInfo* macroBig = compiler.getPreprocessor().getMacroInfo(
34 &compiler.getPreprocessor().getIdentifierTable().get("OSL_BIGENDIAN"))) {
35 definedBig_ = macroBig->getDefinitionLoc();
37 if(const MacroInfo* macroLit = compiler.getPreprocessor().getMacroInfo(
38 &compiler.getPreprocessor().getIdentifierTable().get("OSL_LITENDIAN"))) {
39 definedLit_ = macroLit->getDefinitionLoc();
44 void MacroDefined(Token const & MacroNameTok, MacroDirective const *)
45 override
47 auto id = MacroNameTok.getIdentifierInfo()->getName();
48 if (id == "OSL_BIGENDIAN") {
49 if (definedLit_.isValid()) {
50 report(
51 DiagnosticsEngine::Warning,
52 "macro %0 defined in addition to 'OSL_LITENDIAN'",
53 MacroNameTok.getLocation())
54 << MacroNameTok.getIdentifierInfo();
55 report(
56 DiagnosticsEngine::Note,
57 "conflicting macro definition is here", definedLit_);
59 definedBig_ = MacroNameTok.getLocation();
60 assert(definedBig_.isValid());
61 } else if (id == "OSL_LITENDIAN") {
62 if (definedBig_.isValid()) {
63 report(
64 DiagnosticsEngine::Warning,
65 "macro %0 defined in addition to 'OSL_BIGENDIAN'",
66 MacroNameTok.getLocation())
67 << MacroNameTok.getIdentifierInfo();
68 report(
69 DiagnosticsEngine::Note,
70 "conflicting macro definition is here", definedBig_);
72 definedLit_ = MacroNameTok.getLocation();
73 assert(definedLit_.isValid());
77 void MacroUndefined(
78 Token const & MacroNameTok, MacroDefinition const &, MacroDirective const *) override
80 auto id = MacroNameTok.getIdentifierInfo()->getName();
81 if (id == "OSL_BIGENDIAN" || id == "OSL_LITENDIAN") {
82 report(
83 DiagnosticsEngine::Warning, "macro %0 undefinition",
84 MacroNameTok.getLocation())
85 << MacroNameTok.getIdentifierInfo();
89 void Defined(
90 Token const & MacroNameTok, MacroDefinition const &, SourceRange)
91 override
93 check(MacroNameTok);
96 void Ifdef(
97 SourceLocation, Token const & MacroNameTok,
98 MacroDefinition const &) override
100 check(MacroNameTok);
103 void Ifndef(
104 SourceLocation, Token const & MacroNameTok,
105 MacroDefinition const &) override
107 check(MacroNameTok);
110 void check(Token const & macro) const {
111 auto id = macro.getIdentifierInfo()->getName();
112 if ((id == "OSL_BIGENDIAN" || id == "OSL_LITENDIAN")
113 && definedBig_.isInvalid() && definedLit_.isInvalid())
115 report(
116 DiagnosticsEngine::Warning,
117 "definition of macro %0 checked but 'osl/endian.h' is not"
118 " included",
119 macro.getLocation())
120 << macro.getIdentifierInfo();
124 SourceLocation definedBig_;
125 SourceLocation definedLit_;
126 bool startChecked = false;
129 loplugin::Plugin::Registration<OslEndian> X("oslendian");
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */