bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / checkconfigmacros.cxx
blobcff2d41dd6dff025936a78681bdc64b1737ac3c2
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 configMacros.insert( macroToken.getIdentifierInfo()->getName());
74 void CheckConfigMacros::MacroUndefined( const Token& macroToken, MacroDefinition const &, MacroDirective const * )
76 configMacros.erase( macroToken.getIdentifierInfo()->getName());
79 void CheckConfigMacros::Ifdef( SourceLocation location, const Token& macroToken, MacroDefinition const & )
81 checkMacro( macroToken, location );
84 void CheckConfigMacros::Ifndef( SourceLocation location, const Token& macroToken, MacroDefinition const & )
86 checkMacro( macroToken, location );
89 void CheckConfigMacros::Defined( const Token& macroToken, MacroDefinition const &, SourceRange )
91 checkMacro( macroToken, macroToken.getLocation());
94 void CheckConfigMacros::checkMacro( const Token& macroToken, SourceLocation location )
96 if( configMacros.find( macroToken.getIdentifierInfo()->getName()) != configMacros.end())
98 const char* filename = compiler.getSourceManager().getPresumedLoc( location ).getFilename();
99 if( filename == NULL
100 || !hasPathnamePrefix(filename, SRCDIR "/include/LibreOfficeKit/") )
102 report( DiagnosticsEngine::Error, "checking whether a config macro %0 is defined",
103 location ) << macroToken.getIdentifierInfo()->getName();
104 report( DiagnosticsEngine::Note, "use #if instead of #ifdef/#ifndef/defined", location );
109 static Plugin::Registration< CheckConfigMacros > X( "checkconfigmacros" );
111 } // namespace
113 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */