libreoffice/compilerplugins/clang/unusedfieldsremove.cxx

136 lines
4.2 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
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, #include <sys/mman.h>
#include <cassert>
#include <string>
#include <iostream>
#include "plugin.hxx"
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <assert.h>
#include <cstring>
/**
This is intended to be run as the second stage of the "unusedfields" clang plugin.
*/
namespace {
class UnusedFieldsRemove:
public RecursiveASTVisitor<UnusedFieldsRemove>, public loplugin::RewritePlugin
{
public:
explicit UnusedFieldsRemove(loplugin::InstantiationData const & data);
~UnusedFieldsRemove();
virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
bool VisitFieldDecl( const FieldDecl* var );
private:
// I use a brute-force approach - mmap the results file and do a linear search on it
// It works surprisingly well, because the file is small enough to fit into L2 cache on modern CPU's
size_t mmapFilesize;
int mmapFD;
char* mmappedData;
};
size_t getFilesize(const char* filename)
{
struct stat st;
stat(filename, &st);
return st.st_size;
}
UnusedFieldsRemove::UnusedFieldsRemove(loplugin::InstantiationData const & data): RewritePlugin(data)
{
static const char sInputFile[] = SRCDIR "/result.txt";
mmapFilesize = getFilesize(sInputFile);
//Open file
mmapFD = open(sInputFile, O_RDONLY, 0);
assert(mmapFD != -1);
//Execute mmap
mmappedData = static_cast<char*>(mmap(NULL, mmapFilesize, PROT_READ, MAP_PRIVATE, mmapFD, 0));
assert(mmappedData != NULL);
}
UnusedFieldsRemove::~UnusedFieldsRemove()
{
//Cleanup
int rc = munmap(mmappedData, mmapFilesize);
assert(rc == 0);
close(mmapFD);
}
std::string niceName(const FieldDecl* fieldDecl)
{
return fieldDecl->getParent()->getQualifiedNameAsString() + " " +
fieldDecl->getNameAsString();
}
bool UnusedFieldsRemove::VisitFieldDecl( const FieldDecl* fieldDecl )
{
if (rewriter == nullptr) {
return true;
}
if (ignoreLocation(fieldDecl)) {
return true;
}
// ignore stuff that forms part of the stable URE interface
if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(
fieldDecl->getCanonicalDecl()->getLocation()))) {
return true;
}
// don't mess with templates
/* if (isa<CXXRecordDecl>(fieldDecl->getParent())) {
if (dyn_cast<CXXRecordDecl>(fieldDecl->getParent())->getDescribedClassTemplate() != nullptr) {
return true;
}
}
*/
std::string aNiceName = " " + niceName(fieldDecl) + "\n";
const char *aNiceNameStr = aNiceName.c_str();
char* found = std::search(mmappedData, mmappedData + mmapFilesize, aNiceNameStr, aNiceNameStr + strlen(aNiceNameStr));
if(!(found < mmappedData + mmapFilesize)) {
return true;
}
SourceRange replaceRange(fieldDecl->getSourceRange());
// sometimes the declaration has a semicolon just after it, and it's much neater to remove that too.
if (rewriter->getRewrittenText(SourceRange(replaceRange.getEnd(), replaceRange.getEnd().getLocWithOffset(1))) == ";") {
replaceRange.setEnd(replaceRange.getEnd().getLocWithOffset(1));
}
// remove leading spaces
while (rewriter->getRewrittenText(SourceRange(replaceRange.getBegin().getLocWithOffset(-1), replaceRange.getBegin())) == " ")
{
replaceRange.setBegin(replaceRange.getBegin().getLocWithOffset(-1));
}
if (!replaceText(replaceRange, "")) {
report(
DiagnosticsEngine::Warning,
"Could not remove unused field (" + niceName(fieldDecl) + ")",
compat::getBeginLoc(fieldDecl))
<< fieldDecl->getSourceRange();
}
return true;
}
loplugin::Plugin::Registration< UnusedFieldsRemove > X("unusedfieldsremove", false);
}
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
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */