Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / checkconfigmacros.cxx
blobaea60ed561b52faf3b649664108a150ca6b3431a
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 "config_clang.h"
19 #include <clang/Lex/Preprocessor.h>
21 namespace loplugin
25 This is a compile check.
27 Feature macros from config_XXX.h headers are always #defined (to 1 or 0 in case of yes/no
28 settings). It is a mistake to use #ifdef/#ifndef/defined to check them.
30 Using 1/0 instead of defined/undefined avoids undetected problems when e.g. the necessary
31 #include of the config_XXX.h file is missing.
34 class CheckConfigMacros
35 : public PPCallbacks
36 , public Plugin
38 public:
39 explicit CheckConfigMacros( const InstantiationData& data );
40 virtual void run() override;
41 virtual void MacroDefined( const Token& macroToken, const MacroDirective* info ) override;
42 virtual void MacroUndefined( const Token& macroToken, MacroDefinition const &, MacroDirective const * ) override;
43 virtual void Ifdef( SourceLocation location, const Token& macroToken, MacroDefinition const & ) override;
44 virtual void Ifndef( SourceLocation location, const Token& macroToken, MacroDefinition const & ) override;
45 virtual void Defined( const Token& macroToken, MacroDefinition const &, SourceRange Range ) override;
46 enum { isPPCallback = true };
47 private:
48 void checkMacro( const Token& macroToken, SourceLocation location );
49 std::set< std::string > configMacros;
52 CheckConfigMacros::CheckConfigMacros( const InstantiationData& data )
53 : Plugin( data )
55 compiler.getPreprocessor().addPPCallbacks(std::unique_ptr<PPCallbacks>(this));
58 void CheckConfigMacros::run()
60 // nothing, only check preprocessor usage
63 void CheckConfigMacros::MacroDefined( const Token& macroToken, const MacroDirective* info )
65 SourceLocation location = info->getLocation();
66 const char* filename = compiler.getSourceManager().getPresumedLoc( location ).getFilename();
67 if( filename != NULL
68 && ( hasPathnamePrefix(filename, BUILDDIR "/config_host/")
69 || hasPathnamePrefix(filename, BUILDDIR "/config_build/") ))
71 // fprintf(stderr,"DEF: %s %s\n", macroToken.getIdentifierInfo()->getName().data(), filename );
72 StringRef macro = macroToken.getIdentifierInfo()->getName();
73 // Skia #defines do not have values, but we set them in config_skia.h .
74 if( macro.startswith( "SK_" ) && loplugin::isSamePathname(filename, BUILDDIR "/config_host/config_skia.h"))
75 return;
76 configMacros.insert( macro.str());
80 void CheckConfigMacros::MacroUndefined( const Token& macroToken, MacroDefinition const &, MacroDirective const * )
82 configMacros.erase( macroToken.getIdentifierInfo()->getName().str());
85 void CheckConfigMacros::Ifdef( SourceLocation location, const Token& macroToken, MacroDefinition const & )
87 checkMacro( macroToken, location );
90 void CheckConfigMacros::Ifndef( SourceLocation location, const Token& macroToken, MacroDefinition const & )
92 checkMacro( macroToken, location );
95 void CheckConfigMacros::Defined( const Token& macroToken, MacroDefinition const &, SourceRange )
97 checkMacro( macroToken, macroToken.getLocation());
100 void CheckConfigMacros::checkMacro( const Token& macroToken, SourceLocation location )
102 if( configMacros.find( macroToken.getIdentifierInfo()->getName().str()) != configMacros.end())
104 const char* filename = compiler.getSourceManager().getPresumedLoc( location ).getFilename();
105 if( filename == NULL
106 || ( !hasPathnamePrefix(filename, SRCDIR "/include/LibreOfficeKit/")
107 && !hasPathnamePrefix(filename, WORKDIR "/UnpackedTarball/" )))
109 report( DiagnosticsEngine::Error, "checking whether a config macro %0 is defined",
110 location ) << macroToken.getIdentifierInfo()->getName();
111 report( DiagnosticsEngine::Note, "use #if instead of #ifdef/#ifndef/defined", location );
116 static Plugin::Registration< CheckConfigMacros > X( "checkconfigmacros" );
118 } // namespace
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */