1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
7 * This file is distributed under the University of Illinois Open Source
8 * License. See LICENSE.TXT for details.
18 #include "config_clang.h"
20 #include <clang/Lex/Preprocessor.h>
26 This is a compile check.
28 Feature macros from config_XXX.h headers are always #defined (to 1 or 0 in case of yes/no
29 settings). It is a mistake to use #ifdef/#ifndef/defined to check them.
31 Using 1/0 instead of defined/undefined avoids undetected problems when e.g. the necessary
32 #include of the config_XXX.h file is missing.
35 class CheckConfigMacros
40 explicit CheckConfigMacros( const InstantiationData
& data
);
41 virtual void run() override
;
42 virtual void MacroDefined( const Token
& macroToken
, const MacroDirective
* info
) override
;
43 virtual void MacroUndefined( const Token
& macroToken
, MacroDefinition
const &, MacroDirective
const * ) override
;
44 virtual void Ifdef( SourceLocation location
, const Token
& macroToken
, MacroDefinition
const & ) override
;
45 virtual void Ifndef( SourceLocation location
, const Token
& macroToken
, MacroDefinition
const & ) override
;
46 virtual void Defined( const Token
& macroToken
, MacroDefinition
const &, SourceRange Range
) override
;
47 enum { isPPCallback
= true };
49 void checkMacro( const Token
& macroToken
, SourceLocation location
);
50 std::set
< std::string
> configMacros
;
53 CheckConfigMacros::CheckConfigMacros( const InstantiationData
& data
)
56 compiler
.getPreprocessor().addPPCallbacks(std::unique_ptr
<PPCallbacks
>(this));
59 void CheckConfigMacros::run()
61 // nothing, only check preprocessor usage
64 void CheckConfigMacros::MacroDefined( const Token
& macroToken
, const MacroDirective
* info
)
66 SourceLocation location
= info
->getLocation();
67 const char* filename
= compiler
.getSourceManager().getPresumedLoc( location
).getFilename();
69 && ( hasPathnamePrefix(filename
, BUILDDIR
"/config_host/")
70 || hasPathnamePrefix(filename
, BUILDDIR
"/config_build/") ))
72 // fprintf(stderr,"DEF: %s %s\n", macroToken.getIdentifierInfo()->getName().data(), filename );
73 StringRef macro
= macroToken
.getIdentifierInfo()->getName();
74 // Skia #defines do not have values, but we set them in config_skia.h .
75 if( compat::starts_with(macro
, "SK_" ) && loplugin::isSamePathname(filename
, BUILDDIR
"/config_host/config_skia.h"))
77 configMacros
.insert( macro
.str());
81 void CheckConfigMacros::MacroUndefined( const Token
& macroToken
, MacroDefinition
const &, MacroDirective
const * )
83 configMacros
.erase( macroToken
.getIdentifierInfo()->getName().str());
86 void CheckConfigMacros::Ifdef( SourceLocation location
, const Token
& macroToken
, MacroDefinition
const & )
88 checkMacro( macroToken
, location
);
91 void CheckConfigMacros::Ifndef( SourceLocation location
, const Token
& macroToken
, MacroDefinition
const & )
93 checkMacro( macroToken
, location
);
96 void CheckConfigMacros::Defined( const Token
& macroToken
, MacroDefinition
const &, SourceRange
)
98 checkMacro( macroToken
, macroToken
.getLocation());
101 void CheckConfigMacros::checkMacro( const Token
& macroToken
, SourceLocation location
)
103 if( configMacros
.find( macroToken
.getIdentifierInfo()->getName().str()) != configMacros
.end())
105 const char* filename
= compiler
.getSourceManager().getPresumedLoc( location
).getFilename();
107 || ( !hasPathnamePrefix(filename
, SRCDIR
"/include/LibreOfficeKit/")
108 && !hasPathnamePrefix(filename
, WORKDIR
"/UnpackedTarball/" )))
110 report( DiagnosticsEngine::Error
, "checking whether a config macro %0 is defined",
111 location
) << macroToken
.getIdentifierInfo()->getName();
112 report( DiagnosticsEngine::Note
, "use #if instead of #ifdef/#ifndef/defined", location
);
117 static Plugin::Registration
< CheckConfigMacros
> X( "checkconfigmacros" );
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */