2013-09-21 14:42:35 +01:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2013-02-02 16:35:47 +01:00
/*
* This file is part of the LibreOffice project .
*
* Based on LLVM / Clang .
*
* This file is distributed under the University of Illinois Open Source
* License . See LICENSE . TXT for details .
*
*/
2017-04-14 08:42:15 +10:00
# include <memory>
2017-12-15 14:20:38 +01:00
# include <system_error>
2019-08-21 09:01:12 +02:00
# include <utility>
2017-12-15 14:20:38 +01:00
2022-02-15 15:03:24 +01:00
# include "config_clang.h"
2017-11-07 11:19:30 +01:00
# include "plugin.hxx"
2013-02-02 16:35:47 +01:00
# include "pluginhandler.hxx"
# include <clang/Frontend/CompilerInstance.h>
# include <clang/Frontend/FrontendPluginRegistry.h>
2013-04-04 12:52:04 +02:00
# include <clang/Lex/PPCallbacks.h>
2022-11-05 15:55:28 +01:00
# include <llvm/ADT/StringExtras.h>
2020-01-28 22:10:10 +01:00
# include <llvm/Support/TimeProfiler.h>
2016-12-18 14:20:19 +01:00
# if defined _WIN32
# include <process.h>
# else
2013-02-09 18:47:55 +01:00
# include <sys/stat.h>
2013-02-02 16:35:47 +01:00
# include <unistd.h>
2016-12-18 14:20:19 +01:00
# endif
2013-02-02 16:35:47 +01:00
2017-06-20 10:26:46 +02:00
/**
2013-02-02 18:34:12 +01:00
This source file manages all plugin actions . It is not necessary to modify this
file when adding new actions .
*/
2013-06-05 23:32:16 +02:00
2017-06-20 10:26:46 +02:00
static bool isPrefix ( const std : : string & prefix , const std : : string & full )
2013-06-05 23:32:16 +02:00
{
return full . compare ( 0 , prefix . size ( ) , prefix ) = = 0 ;
}
2013-02-02 16:35:47 +01:00
namespace loplugin
{
2013-02-02 17:45:18 +01:00
struct PluginData
2017-06-20 10:26:46 +02:00
{
2017-11-07 11:19:30 +01:00
Plugin * ( * create ) ( const InstantiationData & ) ;
2013-02-02 17:45:18 +01:00
Plugin * object ;
const char * optionName ;
2013-08-06 17:57:45 +02:00
bool isPPCallback ;
2019-03-07 14:31:17 +01:00
bool isSharedPlugin ;
2014-01-27 13:09:20 +01:00
bool byDefault ;
2019-03-07 14:31:17 +01:00
bool disabledRun ;
2017-06-20 10:26:46 +02:00
} ;
2013-02-02 17:45:18 +01:00
2018-07-25 13:55:35 +02:00
const int MAX_PLUGINS = 200 ;
2013-02-02 17:45:18 +01:00
static PluginData plugins [ MAX_PLUGINS ] ;
static int pluginCount = 0 ;
2015-09-25 11:41:53 +01:00
static bool bPluginObjectsCreated = false ;
2017-06-19 13:44:14 +02:00
static bool unitTestMode = false ;
2013-02-02 17:45:18 +01:00
2018-05-29 11:28:07 +02:00
StringRef initMainFileName ( CompilerInstance & compiler )
{
StringRef const & fn ( compiler . getASTContext ( ) . getSourceManager ( ) . getFileEntryForID (
compiler . getASTContext ( ) . getSourceManager ( ) . getMainFileID ( ) ) - > getName ( ) ) ;
if ( fn = = " <stdin> " )
// stdin means icecream, so we can rely on -main-file-name containing the full path name
return compiler . getCodeGenOpts ( ) . MainFileName ;
else
// this is always a full path name
return fn ;
}
2017-06-20 10:26:46 +02:00
PluginHandler : : PluginHandler ( CompilerInstance & compiler , const std : : vector < std : : string > & args )
2013-03-21 16:42:10 +01:00
: compiler ( compiler )
2018-05-29 11:28:07 +02:00
, mainFileName ( initMainFileName ( compiler ) )
2013-03-21 16:42:10 +01:00
, rewriter ( compiler . getSourceManager ( ) , compiler . getLangOpts ( ) )
2013-02-09 18:47:55 +01:00
, scope ( " mainfile " )
2016-04-26 17:33:47 +02:00
, warningsAsErrors ( false )
2017-06-20 10:26:46 +02:00
{
std : : set < std : : string > rewriters ;
for ( std : : string const & arg : args )
2013-02-02 17:45:18 +01:00
{
2017-06-19 09:00:59 +02:00
if ( arg . size ( ) > = 2 & & arg [ 0 ] = = ' - ' & & arg [ 1 ] = = ' - ' )
handleOption ( arg . substr ( 2 ) ) ;
2013-02-09 18:47:55 +01:00
else
2017-06-19 09:00:59 +02:00
rewriters . insert ( arg ) ;
2013-02-02 17:45:18 +01:00
}
2014-02-15 15:25:03 +01:00
createPlugins ( rewriters ) ;
2015-09-25 11:41:53 +01:00
bPluginObjectsCreated = true ;
2017-06-20 10:26:46 +02:00
}
2013-02-02 17:45:18 +01:00
PluginHandler : : ~ PluginHandler ( )
2017-06-20 10:26:46 +02:00
{
for ( int i = 0 ; i < pluginCount ; + + i )
2013-02-02 17:45:18 +01:00
if ( plugins [ i ] . object ! = NULL )
2017-06-20 10:26:46 +02:00
{
2013-04-04 12:52:04 +02:00
// PPCallbacks is owned by preprocessor object, don't delete those
2013-08-06 17:57:45 +02:00
if ( ! plugins [ i ] . isPPCallback )
2013-04-04 12:52:04 +02:00
delete plugins [ i ] . object ;
2017-06-20 10:26:46 +02:00
}
}
2013-02-02 17:45:18 +01:00
2017-06-19 13:44:14 +02:00
bool PluginHandler : : isUnitTestMode ( )
2017-06-20 10:26:46 +02:00
{
2017-06-19 13:44:14 +02:00
return unitTestMode ;
2017-06-20 10:26:46 +02:00
}
2017-06-19 13:44:14 +02:00
2017-06-20 10:26:46 +02:00
void PluginHandler : : handleOption ( const std : : string & option )
{
2013-02-09 18:47:55 +01:00
if ( option . substr ( 0 , 6 ) = = " scope= " )
2017-06-20 10:26:46 +02:00
{
2013-02-09 18:47:55 +01:00
scope = option . substr ( 6 ) ;
if ( scope = = " mainfile " | | scope = = " all " )
; // ok
else
2017-06-20 10:26:46 +02:00
{
Support loplugin in clang-cl
This works at least with a recent Clang trunk (towards Clang 6.0).
In order for the plugin.dll to find the LLVM/Clang symbols, it needs to be
loaded into clang.exe not clang-cl.exe, so set CC/CXX to 'clang.exe
--driver-mode=cl ...'.
Buidling the plugin requires some linker flags that must go at the very end of
the COMPILER_PLUGINS_CXX command line, after a /link switch, so introduce
another COMPILER_PLUGINS_CXX_LINKFLAGS variable for that. Also, clang.lib is
not installed as part of LLVM's 'cmake --build ... --target install' step, so
is not available under CLANGDIR and needs to be taken from the build tree
instead, so introduce another CLANGLIBDIR variable for that. autogen.input
settings that work for me on Windows 8.1 with Microsoft Visual Studio 14.0 are:
> CLANGDIR=C:/llvm/inst
> CLANGLIBDIR=C:/llvm/build/lib
> COMPILER_PLUGINS_CXX=C:/PROGRA~2/MICROS~3.0/VC/bin/amd64/cl.exe /IC:\PROGRA~2\MICROS~3.0\VC\INCLUDE /IC:\PROGRA~2\MICROS~3.0\VC\ATLMFC\INCLUDE /IC:\PROGRA~2\WI3CF2~1\10\include\100102~1.0\ucrt /IC:\PROGRA~2\WI3CF2~1\NETFXSDK\46D346~1.1\include\um /IC:\PROGRA~2\WI3CF2~1\8.1\include\shared /IC:\PROGRA~2\WI3CF2~1\8.1\include\um /IC:\PROGRA~2\WI3CF2~1\8.1\include\winrt
> COMPILER_PLUGINS_CXX_LINKFLAGS=/LIBPATH:C:/PROGRA~2/MICROS~3.0/VC/LIB/amd64 /LIBPATH:C:/PROGRA~2/MICROS~3.0/VC/ATLMFC/LIB/amd64 /LIBPATH:C:/PROGRA~2/WI3CF2~1/10/lib/100102~1.0/ucrt/x64 /LIBPATH:C:/PROGRA~2/WI3CF2~1/NETFXSDK/46D346~1.1/lib/um/x64 /LIBPATH:C:/PROGRA~2/WI3CF2~1/8.1/lib/winv6.3/um/x64
(The last two are "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/
amd64/cl.exe" and translations of %INCLUDE% and %LIB% as set in the "VS2015 x64
Native Tools Command Prompt" shell.
AC_CHECK_HEADER(clang/AST/RecursiveASTVisitor.h, ...) in configure.ac wouldn't
like CXX to start with INCLUDE=... LIB=... environment variable settings, so it
wouldn't work to instead pass %INCLUDE% and %LIB% to cl.exe that way. See
<https://wiki.documentfoundation.org/Development/clang-cl> for general
information about building with clang-cl on Windows.)
There's still some room for improvement marked "TODO". (And some of the unused*
plugins, which are not run by default anyway, use Unix-style functionality, so
have been disabled for now.)
Change-Id: I6c28bdeb801af39ce2bae03111f455e2338d66c9
Reviewed-on: https://gerrit.libreoffice.org/42931
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2017-09-29 08:37:45 +02:00
# if !defined _WIN32 //TODO, S_ISDIR
2013-02-09 18:47:55 +01:00
struct stat st ;
if ( stat ( ( SRCDIR " / " + scope ) . c_str ( ) , & st ) ! = 0 | | ! S_ISDIR ( st . st_mode ) )
report ( DiagnosticsEngine : : Fatal , " unknown scope %0 (no such module directory) " ) < < scope ;
Support loplugin in clang-cl
This works at least with a recent Clang trunk (towards Clang 6.0).
In order for the plugin.dll to find the LLVM/Clang symbols, it needs to be
loaded into clang.exe not clang-cl.exe, so set CC/CXX to 'clang.exe
--driver-mode=cl ...'.
Buidling the plugin requires some linker flags that must go at the very end of
the COMPILER_PLUGINS_CXX command line, after a /link switch, so introduce
another COMPILER_PLUGINS_CXX_LINKFLAGS variable for that. Also, clang.lib is
not installed as part of LLVM's 'cmake --build ... --target install' step, so
is not available under CLANGDIR and needs to be taken from the build tree
instead, so introduce another CLANGLIBDIR variable for that. autogen.input
settings that work for me on Windows 8.1 with Microsoft Visual Studio 14.0 are:
> CLANGDIR=C:/llvm/inst
> CLANGLIBDIR=C:/llvm/build/lib
> COMPILER_PLUGINS_CXX=C:/PROGRA~2/MICROS~3.0/VC/bin/amd64/cl.exe /IC:\PROGRA~2\MICROS~3.0\VC\INCLUDE /IC:\PROGRA~2\MICROS~3.0\VC\ATLMFC\INCLUDE /IC:\PROGRA~2\WI3CF2~1\10\include\100102~1.0\ucrt /IC:\PROGRA~2\WI3CF2~1\NETFXSDK\46D346~1.1\include\um /IC:\PROGRA~2\WI3CF2~1\8.1\include\shared /IC:\PROGRA~2\WI3CF2~1\8.1\include\um /IC:\PROGRA~2\WI3CF2~1\8.1\include\winrt
> COMPILER_PLUGINS_CXX_LINKFLAGS=/LIBPATH:C:/PROGRA~2/MICROS~3.0/VC/LIB/amd64 /LIBPATH:C:/PROGRA~2/MICROS~3.0/VC/ATLMFC/LIB/amd64 /LIBPATH:C:/PROGRA~2/WI3CF2~1/10/lib/100102~1.0/ucrt/x64 /LIBPATH:C:/PROGRA~2/WI3CF2~1/NETFXSDK/46D346~1.1/lib/um/x64 /LIBPATH:C:/PROGRA~2/WI3CF2~1/8.1/lib/winv6.3/um/x64
(The last two are "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/
amd64/cl.exe" and translations of %INCLUDE% and %LIB% as set in the "VS2015 x64
Native Tools Command Prompt" shell.
AC_CHECK_HEADER(clang/AST/RecursiveASTVisitor.h, ...) in configure.ac wouldn't
like CXX to start with INCLUDE=... LIB=... environment variable settings, so it
wouldn't work to instead pass %INCLUDE% and %LIB% to cl.exe that way. See
<https://wiki.documentfoundation.org/Development/clang-cl> for general
information about building with clang-cl on Windows.)
There's still some room for improvement marked "TODO". (And some of the unused*
plugins, which are not run by default anyway, use Unix-style functionality, so
have been disabled for now.)
Change-Id: I6c28bdeb801af39ce2bae03111f455e2338d66c9
Reviewed-on: https://gerrit.libreoffice.org/42931
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2017-09-29 08:37:45 +02:00
# endif
2013-02-09 18:47:55 +01:00
}
2017-06-20 10:26:46 +02:00
}
2014-01-27 13:09:20 +01:00
else if ( option . substr ( 0 , 14 ) = = " warnings-only= " )
2017-06-20 10:26:46 +02:00
{
2014-01-27 13:09:20 +01:00
warningsOnly = option . substr ( 14 ) ;
2017-06-20 10:26:46 +02:00
}
2016-04-26 17:33:47 +02:00
else if ( option = = " warnings-as-errors " )
warningsAsErrors = true ;
2017-06-19 09:00:59 +02:00
else if ( option = = " unit-test-mode " )
unitTestMode = true ;
2017-12-08 16:31:34 +01:00
else if ( option = = " debug " )
debugMode = true ;
2013-02-09 18:47:55 +01:00
else
report ( DiagnosticsEngine : : Fatal , " unknown option %0 " ) < < option ;
2017-06-20 10:26:46 +02:00
}
2013-02-09 18:47:55 +01:00
2017-06-20 10:26:46 +02:00
void PluginHandler : : createPlugins ( std : : set < std : : string > rewriters )
{
for ( int i = 0 ; i < pluginCount ; + + i )
2013-02-09 18:47:55 +01:00
{
2017-06-19 13:44:14 +02:00
const char * name = plugins [ i ] . optionName ;
2017-10-19 21:33:08 +02:00
// When in unit-test mode, ignore plugins whose names don't match the filename of the test,
// so that we only generate warnings for the plugin that we want to test.
2020-01-27 22:25:13 +01:00
// Sharedvisitor plugins still need to remain enabled, they don't do anything on their own,
// but sharing-capable plugins need them to actually work (if compiled so) and they register
// with them in the code below.
if ( unitTestMode & & mainFileName . find ( plugins [ i ] . optionName ) = = StringRef : : npos
& & ! plugins [ i ] . isSharedPlugin )
2017-10-19 21:33:08 +02:00
continue ;
2017-06-19 13:44:14 +02:00
if ( rewriters . erase ( name ) ! = 0 )
2017-11-07 11:19:30 +01:00
plugins [ i ] . object = plugins [ i ] . create ( InstantiationData { name , * this , compiler , & rewriter } ) ;
2014-02-15 15:25:03 +01:00
else if ( plugins [ i ] . byDefault )
2017-11-07 11:19:30 +01:00
plugins [ i ] . object = plugins [ i ] . create ( InstantiationData { name , * this , compiler , NULL } ) ;
2017-06-19 13:44:14 +02:00
else if ( unitTestMode & & strcmp ( name , " unusedmethodsremove " ) ! = 0 & & strcmp ( name , " unusedfieldsremove " ) ! = 0 )
2017-11-07 11:19:30 +01:00
plugins [ i ] . object = plugins [ i ] . create ( InstantiationData { name , * this , compiler , NULL } ) ;
2017-06-20 10:26:46 +02:00
}
2014-02-15 15:25:03 +01:00
for ( auto r : rewriters )
report ( DiagnosticsEngine : : Fatal , " unknown plugin tool %0 " ) < < r ;
2019-03-07 14:31:17 +01:00
// If there is a shared plugin, make it handle all plugins that it can handle.
for ( int i = 0 ; i < pluginCount ; + + i )
{
if ( plugins [ i ] . isSharedPlugin & & plugins [ i ] . object ! = nullptr )
{
Plugin * plugin = plugins [ i ] . object ;
for ( int j = 0 ; j < pluginCount ; + + j )
{
if ( plugins [ j ] . object ! = nullptr
& & plugin - > setSharedPlugin ( plugins [ j ] . object , plugins [ j ] . optionName ) )
{
plugins [ j ] . disabledRun = true ;
}
}
}
}
2017-06-20 10:26:46 +02:00
}
2013-02-09 18:47:55 +01:00
2019-03-07 14:31:17 +01:00
void PluginHandler : : registerPlugin ( Plugin * ( * create ) ( const InstantiationData & ) , const char * optionName ,
bool isPPCallback , bool isSharedPlugin , bool byDefault )
2017-06-20 10:26:46 +02:00
{
2015-09-25 11:41:53 +01:00
assert ( ! bPluginObjectsCreated ) ;
2013-02-02 17:45:18 +01:00
assert ( pluginCount < MAX_PLUGINS ) ;
plugins [ pluginCount ] . create = create ;
plugins [ pluginCount ] . object = NULL ;
plugins [ pluginCount ] . optionName = optionName ;
2013-08-06 17:57:45 +02:00
plugins [ pluginCount ] . isPPCallback = isPPCallback ;
2019-03-07 14:31:17 +01:00
plugins [ pluginCount ] . isSharedPlugin = isSharedPlugin ;
2014-01-27 13:09:20 +01:00
plugins [ pluginCount ] . byDefault = byDefault ;
2019-03-07 14:31:17 +01:00
plugins [ pluginCount ] . disabledRun = false ;
2013-02-02 17:45:18 +01:00
+ + pluginCount ;
2017-06-20 10:26:46 +02:00
}
2013-02-02 16:35:47 +01:00
2014-01-27 13:09:20 +01:00
DiagnosticBuilder PluginHandler : : report ( DiagnosticsEngine : : Level level , const char * plugin , StringRef message , CompilerInstance & compiler ,
SourceLocation loc )
2017-06-20 10:26:46 +02:00
{
2014-01-27 13:09:20 +01:00
DiagnosticsEngine & diag = compiler . getDiagnostics ( ) ;
// Do some mappings (e.g. for -Werror) that clang does not do for custom messages for some reason.
2016-04-26 17:33:47 +02:00
if ( level = = DiagnosticsEngine : : Warning & & ( ( diag . getWarningsAsErrors ( ) & & ( plugin = = nullptr | | plugin ! = warningsOnly ) ) | | warningsAsErrors ) )
2014-01-27 13:09:20 +01:00
level = DiagnosticsEngine : : Error ;
if ( level = = DiagnosticsEngine : : Error & & diag . getErrorsAsFatal ( ) )
level = DiagnosticsEngine : : Fatal ;
2017-06-20 10:26:46 +02:00
std : : string fullMessage = ( message + " [loplugin " ) . str ( ) ;
2014-01-27 13:09:20 +01:00
if ( plugin )
2017-06-20 10:26:46 +02:00
{
2014-01-27 13:09:20 +01:00
fullMessage + = " : " ;
fullMessage + = plugin ;
2017-06-20 10:26:46 +02:00
}
2014-01-27 13:09:20 +01:00
fullMessage + = " ] " ;
if ( loc . isValid ( ) )
2017-12-15 14:20:38 +01:00
return diag . Report ( loc , diag . getDiagnosticIDs ( ) - > getCustomDiagID ( static_cast < DiagnosticIDs : : Level > ( level ) , fullMessage ) ) ;
2014-01-27 13:09:20 +01:00
else
2017-12-15 14:20:38 +01:00
return diag . Report ( diag . getDiagnosticIDs ( ) - > getCustomDiagID ( static_cast < DiagnosticIDs : : Level > ( level ) , fullMessage ) ) ;
2017-06-20 10:26:46 +02:00
}
2014-01-27 13:09:20 +01:00
2013-02-02 19:31:24 +01:00
DiagnosticBuilder PluginHandler : : report ( DiagnosticsEngine : : Level level , StringRef message , SourceLocation loc )
2017-06-20 10:26:46 +02:00
{
2014-01-27 13:09:20 +01:00
return report ( level , nullptr , message , compiler , loc ) ;
2017-06-20 10:26:46 +02:00
}
2013-02-02 19:31:24 +01:00
2017-11-07 11:19:30 +01:00
bool PluginHandler : : ignoreLocation ( SourceLocation loc ) {
auto i = ignored_ . find ( loc ) ;
if ( i = = ignored_ . end ( ) ) {
i = ignored_ . emplace ( loc , checkIgnoreLocation ( loc ) ) . first ;
}
return i - > second ;
}
bool PluginHandler : : checkIgnoreLocation ( SourceLocation loc )
{
2022-06-10 13:11:13 +02:00
// The tree-wide analysis plugins (like unusedmethods) don't want
2022-06-14 11:55:22 +02:00
// this logic, they only want to ignore external code
2022-06-10 13:11:13 +02:00
if ( ! treeWideAnalysisMode )
2021-04-08 17:01:15 +02:00
{
2022-06-10 13:11:13 +02:00
// If a location comes from a PCH, it is not necessary to check it
// in every compilation using the PCH, since with Clang we use
// -building-pch-with-obj to build a separate precompiled_foo.cxx file
// for the PCH, and so it is known that everything in the PCH will
// be checked while compiling this file. Skip the checks for all
// other files using the PCH.
if ( ! compiler . getSourceManager ( ) . isLocalSourceLocation ( loc ) )
{
if ( ! compiler . getLangOpts ( ) . BuildingPCHWithObjectFile )
return true ;
}
2021-04-08 17:01:15 +02:00
}
2017-11-07 11:19:30 +01:00
SourceLocation expansionLoc = compiler . getSourceManager ( ) . getExpansionLoc ( loc ) ;
if ( compiler . getSourceManager ( ) . isInSystemHeader ( expansionLoc ) )
return true ;
2019-02-24 17:46:22 +01:00
PresumedLoc presumedLoc = compiler . getSourceManager ( ) . getPresumedLoc ( expansionLoc ) ;
if ( presumedLoc . isInvalid ( ) )
return true ;
const char * bufferName = presumedLoc . getFilename ( ) ;
2018-03-23 10:49:57 +01:00
if ( bufferName = = NULL
2017-11-07 11:19:30 +01:00
| | hasPathnamePrefix ( bufferName , SRCDIR " /external/ " )
| | isSamePathname ( bufferName , SRCDIR " /sdext/source/pdfimport/wrapper/keyword_list " ) )
// workdir/CustomTarget/sdext/pdfimport/hash.cxx is generated from
// sdext/source/pdfimport/wrapper/keyword_list by gperf, which
// inserts various #line directives denoting the latter into the
// former, but fails to add a #line directive returning back to
// hash.cxx itself before the gperf generated boilerplate, so
// compilers erroneously consider errors in the boilerplate to come
// from keyword_list instead of hash.cxx (for Clang on Linux/macOS
// this is not an issue due to the '#pragma GCC system_header'
// generated into the start of hash.cxx, #if'ed for __GNUC__, but
// for clang-cl it is an issue)
return true ;
2018-03-26 13:37:06 +02:00
if ( hasPathnamePrefix ( bufferName , WORKDIR " / " ) )
2017-11-07 11:19:30 +01:00
{
// workdir/CustomTarget/vcl/unx/kde4/tst_exclude_socket_notifiers.moc
// includes
// "../../../../../vcl/unx/kde4/tst_exclude_socket_notifiers.hxx",
// making the latter file erroneously match here; so strip any ".."
// segments:
if ( strstr ( bufferName , " /.. " ) = = nullptr ) {
return true ;
}
std : : string s ( bufferName ) ;
normalizeDotDotInFilePath ( s ) ;
2018-03-26 13:37:06 +02:00
if ( hasPathnamePrefix ( s , WORKDIR " / " ) )
2017-11-07 11:19:30 +01:00
return true ;
}
2018-03-26 13:37:06 +02:00
if ( hasPathnamePrefix ( bufferName , BUILDDIR " / " )
| | hasPathnamePrefix ( bufferName , SRCDIR " / " ) )
2017-11-07 11:19:30 +01:00
return false ; // ok
return true ;
}
2018-02-09 15:28:41 +02:00
// If we overlap with a previous area we modified, we cannot perform this change
// without corrupting the source
bool PluginHandler : : checkOverlap ( SourceRange range )
2017-06-20 10:26:46 +02:00
{
2018-02-09 15:28:41 +02:00
SourceManager & SM = compiler . getSourceManager ( ) ;
char const * p1 = SM . getCharacterData ( range . getBegin ( ) ) ;
char const * p2 = SM . getCharacterData ( range . getEnd ( ) ) ;
for ( std : : pair < char const * , char const * > const & rPair : mvModifiedRanges )
{
if ( rPair . first < = p1 & & p1 < = rPair . second )
return false ;
if ( p1 < = rPair . second & & rPair . first < = p2 )
return false ;
}
return true ;
2017-06-20 10:26:46 +02:00
}
2014-02-20 19:47:01 +01:00
2018-02-12 10:23:59 +02:00
void PluginHandler : : addSourceModification ( SourceRange range )
{
SourceManager & SM = compiler . getSourceManager ( ) ;
char const * p1 = SM . getCharacterData ( range . getBegin ( ) ) ;
char const * p2 = SM . getCharacterData ( range . getEnd ( ) ) ;
mvModifiedRanges . emplace_back ( p1 , p2 ) ;
}
2013-02-02 16:35:47 +01:00
void PluginHandler : : HandleTranslationUnit ( ASTContext & context )
2017-06-20 10:26:46 +02:00
{
2020-01-28 22:10:10 +01:00
llvm : : TimeTraceScope mainTimeScope ( " LOPluginMain " , StringRef ( " " ) ) ;
2013-02-02 16:35:47 +01:00
if ( context . getDiagnostics ( ) . hasErrorOccurred ( ) )
return ;
2016-10-16 19:43:15 +02:00
if ( mainFileName . endswith ( " .ii " ) )
2015-10-30 15:15:46 +01:00
{
report ( DiagnosticsEngine : : Fatal ,
" input file has suffix .ii: \" %0 \" \n highly suspicious, probably ccache generated, this will break warning suppressions; export CCACHE_CPP2=1 to prevent this " ) < < mainFileName ;
return ;
}
2017-06-20 10:26:46 +02:00
for ( int i = 0 ; i < pluginCount ; + + i )
{
2019-03-07 14:31:17 +01:00
if ( plugins [ i ] . object ! = NULL & & ! plugins [ i ] . disabledRun )
2017-06-20 10:26:46 +02:00
{
2020-01-28 22:10:10 +01:00
llvm : : TimeTraceScope timeScope ( " LOPlugin " , [ & ] ( ) { return plugins [ i ] . optionName ; } ) ;
2017-10-19 21:33:08 +02:00
plugins [ i ] . object - > run ( ) ;
2013-02-02 16:35:47 +01:00
}
2017-06-20 10:26:46 +02:00
}
2017-01-27 10:46:15 +01:00
# if defined _WIN32
//TODO: make the call to 'rename' work on Windows (where the renamed-to
// original file is probably still held open somehow):
rewriter . overwriteChangedFiles ( ) ;
# else
2013-02-02 16:35:47 +01:00
for ( Rewriter : : buffer_iterator it = rewriter . buffer_begin ( ) ;
it ! = rewriter . buffer_end ( ) ;
+ + it )
2017-06-20 10:26:46 +02:00
{
2013-02-02 16:35:47 +01:00
const FileEntry * e = context . getSourceManager ( ) . getFileEntryForID ( it - > first ) ;
2013-08-14 19:00:21 +02:00
if ( e = = NULL )
continue ; // Failed modification because of a macro expansion?
2013-02-02 16:35:47 +01:00
/* Check where the file actually is, and warn about cases where modification
most probably doesn ' t matter ( generated files in workdir ) .
2013-10-31 14:02:40 +01:00
The order here is important , as INSTDIR and WORKDIR are often in SRCDIR / BUILDDIR ,
2013-02-02 16:35:47 +01:00
and BUILDDIR is sometimes in SRCDIR . */
2017-06-20 10:26:46 +02:00
std : : string modifyFile ;
2013-02-09 18:47:55 +01:00
const char * pathWarning = NULL ;
2015-09-25 11:41:53 +01:00
bool bSkip = false ;
2016-10-16 19:43:15 +02:00
StringRef const name = e - > getName ( ) ;
if ( name . startswith ( WORKDIR " / " ) )
2013-02-09 18:47:55 +01:00
pathWarning = " modified source in workdir/ : %0 " ;
2016-10-16 19:43:15 +02:00
else if ( strcmp ( SRCDIR , BUILDDIR ) ! = 0 & & name . startswith ( BUILDDIR " / " ) )
2013-02-09 18:47:55 +01:00
pathWarning = " modified source in build dir : %0 " ;
2016-10-16 19:43:15 +02:00
else if ( name . startswith ( SRCDIR " / " ) )
2013-02-02 16:35:47 +01:00
; // ok
else
2017-06-20 10:26:46 +02:00
{
2013-02-09 18:47:55 +01:00
pathWarning = " modified source in unknown location, not modifying : %0 " ;
2015-09-25 11:41:53 +01:00
bSkip = true ;
2017-06-20 10:26:46 +02:00
}
2013-02-02 16:35:47 +01:00
if ( modifyFile . empty ( ) )
2020-01-29 13:26:26 +01:00
modifyFile = name . str ( ) ;
2013-10-31 14:02:40 +01:00
// Check whether the modified file is in the wanted scope
2013-02-09 18:47:55 +01:00
if ( scope = = " mainfile " )
2017-06-20 10:26:46 +02:00
{
2013-02-09 18:47:55 +01:00
if ( it - > first ! = context . getSourceManager ( ) . getMainFileID ( ) )
continue ;
2017-06-20 10:26:46 +02:00
}
2013-02-09 18:47:55 +01:00
else if ( scope = = " all " )
; // ok
else // scope is module
2017-06-20 10:26:46 +02:00
{
2013-06-05 23:32:16 +02:00
if ( ! ( isPrefix ( SRCDIR " / " + scope + " / " , modifyFile ) | | isPrefix ( SRCDIR " /include/ " + scope + " / " , modifyFile ) ) )
2013-02-09 18:47:55 +01:00
continue ;
2017-06-20 10:26:46 +02:00
}
2013-02-09 18:47:55 +01:00
// Warn only now, so that files not in scope do not cause warnings.
if ( pathWarning ! = NULL )
2016-10-16 19:43:15 +02:00
report ( DiagnosticsEngine : : Warning , pathWarning ) < < name ;
2015-09-25 11:41:53 +01:00
if ( bSkip )
2013-02-09 18:47:55 +01:00
continue ;
2022-11-05 15:55:28 +01:00
auto const filename = modifyFile + " .new. " + itostr ( getpid ( ) ) ;
2017-06-20 10:26:46 +02:00
std : : string error ;
2015-09-25 11:41:53 +01:00
bool bOk = false ;
2017-12-15 14:20:38 +01:00
std : : error_code ec ;
2014-02-25 10:15:28 +01:00
std : : unique_ptr < raw_fd_ostream > ostream (
2022-02-15 15:03:24 +01:00
new raw_fd_ostream ( filename , ec , sys : : fs : : OF_None ) ) ;
2017-12-15 14:20:38 +01:00
if ( ! ec )
2017-06-20 10:26:46 +02:00
{
2014-02-25 10:15:28 +01:00
it - > second . write ( * ostream ) ;
ostream - > close ( ) ;
2022-11-05 15:55:28 +01:00
if ( ! ostream - > has_error ( ) & & rename ( filename . c_str ( ) , modifyFile . c_str ( ) ) = = 0 )
2015-09-25 11:41:53 +01:00
bOk = true ;
2017-06-20 10:26:46 +02:00
}
2017-12-15 14:20:38 +01:00
else
error = " error: " + ec . message ( ) ;
2014-02-25 10:15:28 +01:00
ostream - > clear_error ( ) ;
2022-11-05 15:55:28 +01:00
unlink ( filename . c_str ( ) ) ;
2015-09-25 11:41:53 +01:00
if ( ! bOk )
2013-02-02 19:38:56 +01:00
report ( DiagnosticsEngine : : Error , " cannot write modified source to %0 (%1) " ) < < modifyFile < < error ;
2013-02-02 16:35:47 +01:00
}
2017-06-20 10:26:46 +02:00
# endif
}
2013-02-02 16:35:47 +01:00
2019-12-04 14:32:15 +01:00
namespace {
// BEGIN code copied from LLVM's clang/lib/Sema/Sema.cpp
/// Returns true, if all methods and nested classes of the given
/// CXXRecordDecl are defined in this translation unit.
///
/// Should only be called from ActOnEndOfTranslationUnit so that all
/// definitions are actually read.
static bool MethodsAndNestedClassesComplete ( const CXXRecordDecl * RD ,
RecordCompleteMap & MNCComplete ) {
RecordCompleteMap : : iterator Cache = MNCComplete . find ( RD ) ;
if ( Cache ! = MNCComplete . end ( ) )
return Cache - > second ;
if ( ! RD - > isCompleteDefinition ( ) )
return false ;
bool Complete = true ;
for ( DeclContext : : decl_iterator I = RD - > decls_begin ( ) ,
E = RD - > decls_end ( ) ;
I ! = E & & Complete ; + + I ) {
if ( const CXXMethodDecl * M = dyn_cast < CXXMethodDecl > ( * I ) )
Complete = M - > isDefined ( ) | | M - > isDefaulted ( ) | |
( M - > isPure ( ) & & ! isa < CXXDestructorDecl > ( M ) ) ;
else if ( const FunctionTemplateDecl * F = dyn_cast < FunctionTemplateDecl > ( * I ) )
// If the template function is marked as late template parsed at this
// point, it has not been instantiated and therefore we have not
// performed semantic analysis on it yet, so we cannot know if the type
// can be considered complete.
Complete = ! F - > getTemplatedDecl ( ) - > isLateTemplateParsed ( ) & &
F - > getTemplatedDecl ( ) - > isDefined ( ) ;
else if ( const CXXRecordDecl * R = dyn_cast < CXXRecordDecl > ( * I ) ) {
if ( R - > isInjectedClassName ( ) )
continue ;
if ( R - > hasDefinition ( ) )
Complete = MethodsAndNestedClassesComplete ( R - > getDefinition ( ) ,
MNCComplete ) ;
else
Complete = false ;
}
}
MNCComplete [ RD ] = Complete ;
return Complete ;
}
/// Returns true, if the given CXXRecordDecl is fully defined in this
/// translation unit, i.e. all methods are defined or pure virtual and all
/// friends, friend functions and nested classes are fully defined in this
/// translation unit.
///
/// Should only be called from ActOnEndOfTranslationUnit so that all
/// definitions are actually read.
static bool IsRecordFullyDefined ( const CXXRecordDecl * RD ,
RecordCompleteMap & RecordsComplete ,
RecordCompleteMap & MNCComplete ) {
RecordCompleteMap : : iterator Cache = RecordsComplete . find ( RD ) ;
if ( Cache ! = RecordsComplete . end ( ) )
return Cache - > second ;
bool Complete = MethodsAndNestedClassesComplete ( RD , MNCComplete ) ;
for ( CXXRecordDecl : : friend_iterator I = RD - > friend_begin ( ) ,
E = RD - > friend_end ( ) ;
I ! = E & & Complete ; + + I ) {
// Check if friend classes and methods are complete.
if ( TypeSourceInfo * TSI = ( * I ) - > getFriendType ( ) ) {
// Friend classes are available as the TypeSourceInfo of the FriendDecl.
if ( CXXRecordDecl * FriendD = TSI - > getType ( ) - > getAsCXXRecordDecl ( ) )
Complete = MethodsAndNestedClassesComplete ( FriendD , MNCComplete ) ;
else
Complete = false ;
} else {
// Friend functions are available through the NamedDecl of FriendDecl.
if ( const FunctionDecl * FD =
dyn_cast < FunctionDecl > ( ( * I ) - > getFriendDecl ( ) ) )
Complete = FD - > isDefined ( ) ;
else
// This is a template friend, give up.
Complete = false ;
}
}
RecordsComplete [ RD ] = Complete ;
return Complete ;
}
// END code copied from LLVM's clang/lib/Sema/Sema.cpp
}
bool PluginHandler : : isAllRelevantCodeDefined ( NamedDecl const * decl ) {
switch ( decl - > getAccess ( ) ) {
case AS_protected :
if ( ! cast < CXXRecordDecl > ( decl - > getDeclContext ( ) ) - > hasAttr < FinalAttr > ( ) ) {
break ;
}
LLVM_FALLTHROUGH ;
case AS_private :
if ( IsRecordFullyDefined (
cast < CXXRecordDecl > ( decl - > getDeclContext ( ) ) , RecordsComplete_ , MNCComplete_ ) )
{
return true ;
}
break ;
default :
break ;
}
return ! decl - > isExternallyVisible ( ) ;
}
2014-08-11 15:16:08 +02:00
std : : unique_ptr < ASTConsumer > LibreOfficeAction : : CreateASTConsumer ( CompilerInstance & Compiler , StringRef )
2017-06-20 10:26:46 +02:00
{
2019-08-21 09:01:12 +02:00
# if __cplusplus >= 201402L
return std : : make_unique < PluginHandler > ( Compiler , _args ) ;
# else
2015-09-29 15:04:06 +02:00
return llvm : : make_unique < PluginHandler > ( Compiler , _args ) ;
2019-08-21 09:01:12 +02:00
# endif
2017-06-20 10:26:46 +02:00
}
2013-02-02 16:35:47 +01:00
2017-06-20 10:26:46 +02:00
bool LibreOfficeAction : : ParseArgs ( const CompilerInstance & , const std : : vector < std : : string > & args )
{
2013-02-02 16:35:47 +01:00
_args = args ;
return true ;
2017-06-20 10:26:46 +02:00
}
2013-02-02 16:35:47 +01:00
static FrontendPluginRegistry : : Add < loplugin : : LibreOfficeAction > X ( " loplugin " , " LibreOffice compile check plugin " ) ;
} // namespace
2013-09-21 14:42:35 +01:00
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */