1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
17 class OslEndian
: public loplugin::Plugin
, public PPCallbacks
{
19 explicit OslEndian(loplugin::InstantiationData
const & data
): Plugin(data
) {
20 compiler
.getPreprocessor().addPPCallbacks(std::unique_ptr
<PPCallbacks
>(this));
23 enum { isPPCallback
= true };
26 void run() override
{}
28 virtual void FileChanged(SourceLocation
, FileChangeReason
, SrcMgr::CharacteristicKind
, FileID
) override
{
30 // With precompiled headers MacroDefined() would not be called, so check already at the very
31 // start whether the macros exist.
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 *)
47 auto id
= MacroNameTok
.getIdentifierInfo()->getName();
48 if (id
== "OSL_BIGENDIAN") {
49 if (definedLit_
.isValid()) {
51 DiagnosticsEngine::Warning
,
52 "macro %0 defined in addition to 'OSL_LITENDIAN'",
53 MacroNameTok
.getLocation())
54 << MacroNameTok
.getIdentifierInfo();
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()) {
64 DiagnosticsEngine::Warning
,
65 "macro %0 defined in addition to 'OSL_BIGENDIAN'",
66 MacroNameTok
.getLocation())
67 << MacroNameTok
.getIdentifierInfo();
69 DiagnosticsEngine::Note
,
70 "conflicting macro definition is here", definedBig_
);
72 definedLit_
= MacroNameTok
.getLocation();
73 assert(definedLit_
.isValid());
78 Token
const & MacroNameTok
, MacroDefinition
const &, MacroDirective
const *) override
80 auto id
= MacroNameTok
.getIdentifierInfo()->getName();
81 if (id
== "OSL_BIGENDIAN" || id
== "OSL_LITENDIAN") {
83 DiagnosticsEngine::Warning
, "macro %0 undefinition",
84 MacroNameTok
.getLocation())
85 << MacroNameTok
.getIdentifierInfo();
90 Token
const & MacroNameTok
, MacroDefinition
const &, SourceRange
)
97 SourceLocation
, Token
const & MacroNameTok
,
98 MacroDefinition
const &) override
104 SourceLocation
, Token
const & MacroNameTok
,
105 MacroDefinition
const &) override
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())
116 DiagnosticsEngine::Warning
,
117 "definition of macro %0 checked but 'osl/endian.h' is not"
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: */