tdf#153972 Fix color filter when cells have no content
[LibreOffice.git] / compilerplugins / clang / checkconfigmacros.cxx
blob6daa9ed2a17e76050bd55e27f43b27a639cfde89
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 * Based on LLVM/Clang.
7 * This file is distributed under the University of Illinois Open Source
8 * License. See LICENSE.TXT for details.
12 #include <memory>
13 #include <set>
15 #include "plugin.hxx"
17 #include <clang/Lex/Preprocessor.h>
19 namespace loplugin
23 This is a compile check.
25 Feature macros from config_XXX.h headers are always #defined (to 1 or 0 in case of yes/no
26 settings). It is a mistake to use #ifdef/#ifndef/defined to check them.
28 Using 1/0 instead of defined/undefined avoids undetected problems when e.g. the necessary
29 #include of the config_XXX.h file is missing.
32 class CheckConfigMacros
33 : public PPCallbacks
34 , public Plugin
36 public:
37 explicit CheckConfigMacros( const InstantiationData& data );
38 virtual void run() override;
39 virtual void MacroDefined( const Token& macroToken, const MacroDirective* info ) override;
40 virtual void MacroUndefined( const Token& macroToken, MacroDefinition const &, MacroDirective const * ) override;
41 virtual void Ifdef( SourceLocation location, const Token& macroToken, MacroDefinition const & ) override;
42 virtual void Ifndef( SourceLocation location, const Token& macroToken, MacroDefinition const & ) override;
43 virtual void Defined( const Token& macroToken, MacroDefinition const &, SourceRange Range ) override;
44 enum { isPPCallback = true };
45 private:
46 void checkMacro( const Token& macroToken, SourceLocation location );
47 std::set< std::string > configMacros;
50 CheckConfigMacros::CheckConfigMacros( const InstantiationData& data )
51 : Plugin( data )
53 compiler.getPreprocessor().addPPCallbacks(std::unique_ptr<PPCallbacks>(this));
56 void CheckConfigMacros::run()
58 // nothing, only check preprocessor usage
61 void CheckConfigMacros::MacroDefined( const Token& macroToken, const MacroDirective* info )
63 SourceLocation location = info->getLocation();
64 const char* filename = compiler.getSourceManager().getPresumedLoc( location ).getFilename();
65 if( filename != NULL
66 && ( hasPathnamePrefix(filename, BUILDDIR "/config_host/")
67 || hasPathnamePrefix(filename, BUILDDIR "/config_build/") ))
69 // fprintf(stderr,"DEF: %s %s\n", macroToken.getIdentifierInfo()->getName().data(), filename );
70 StringRef macro = macroToken.getIdentifierInfo()->getName();
71 // Skia #defines do not have values, but we set them in config_skia.h .
72 if( macro.startswith( "SK_" ) && loplugin::isSamePathname(filename, BUILDDIR "/config_host/config_skia.h"))
73 return;
74 configMacros.insert( macro.str());
78 void CheckConfigMacros::MacroUndefined( const Token& macroToken, MacroDefinition const &, MacroDirective const * )
80 configMacros.erase( macroToken.getIdentifierInfo()->getName().str());
83 void CheckConfigMacros::Ifdef( SourceLocation location, const Token& macroToken, MacroDefinition const & )
85 checkMacro( macroToken, location );
88 void CheckConfigMacros::Ifndef( SourceLocation location, const Token& macroToken, MacroDefinition const & )
90 checkMacro( macroToken, location );
93 void CheckConfigMacros::Defined( const Token& macroToken, MacroDefinition const &, SourceRange )
95 checkMacro( macroToken, macroToken.getLocation());
98 void CheckConfigMacros::checkMacro( const Token& macroToken, SourceLocation location )
100 if( configMacros.find( macroToken.getIdentifierInfo()->getName().str()) != configMacros.end())
102 const char* filename = compiler.getSourceManager().getPresumedLoc( location ).getFilename();
103 if( filename == NULL
104 || ( !hasPathnamePrefix(filename, SRCDIR "/include/LibreOfficeKit/")
105 && !hasPathnamePrefix(filename, WORKDIR "/UnpackedTarball/" )))
107 report( DiagnosticsEngine::Error, "checking whether a config macro %0 is defined",
108 location ) << macroToken.getIdentifierInfo()->getName();
109 report( DiagnosticsEngine::Note, "use #if instead of #ifdef/#ifndef/defined", location );
114 static Plugin::Registration< CheckConfigMacros > X( "checkconfigmacros" );
116 } // namespace
118 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */