Move setting of LD_LIBRARY_PATH closer to invocation of cppunittester
[LibreOffice.git] / compilerplugins / clang / store / constfieldsrewrite.cxx
blobd72bb43aad7acf2a6abce265876dc09ba417e2b6
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 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #if !defined _WIN32 //TODO, #include <sys/mman.h>
12 #include <cassert>
13 #include <string>
14 #include <iostream>
15 #include "plugin.hxx"
16 #include "check.hxx"
17 #include "config_clang.h"
18 #include <sys/mman.h>
19 #include <sys/types.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <sys/stat.h>
23 #include <assert.h>
24 #include <cstring>
26 /**
27 This is intended to be run as the second stage of the "constfields" clang plugin.
30 namespace
32 class ConstFieldsRewrite : public RecursiveASTVisitor<ConstFieldsRewrite>,
33 public loplugin::RewritePlugin
35 public:
36 explicit ConstFieldsRewrite(loplugin::InstantiationData const& data);
37 ~ConstFieldsRewrite();
39 virtual void run() override
41 if (rewriter)
43 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
47 bool VisitFieldDecl(const FieldDecl* var);
49 private:
50 // I use a brute-force approach - mmap the results file and do a linear search on it
51 // It works surprisingly well, because the file is small enough to fit into L2 cache on modern CPU's
52 size_t mmapFilesize;
53 int mmapFD;
54 char* mmappedData;
57 size_t getFilesize(const char* filename)
59 struct stat st;
60 stat(filename, &st);
61 return st.st_size;
64 ConstFieldsRewrite::ConstFieldsRewrite(loplugin::InstantiationData const& data)
65 : RewritePlugin(data)
67 static const char sInputFile[] = SRCDIR "/compilerplugins/clang/constfields.results";
68 mmapFilesize = getFilesize(sInputFile);
69 //Open file
70 mmapFD = open(sInputFile, O_RDONLY, 0);
71 assert(mmapFD != -1);
72 //Execute mmap
73 mmappedData = static_cast<char*>(mmap(NULL, mmapFilesize, PROT_READ, MAP_PRIVATE, mmapFD, 0));
74 assert(mmappedData != NULL);
77 ConstFieldsRewrite::~ConstFieldsRewrite()
79 //Cleanup
80 int rc = munmap(mmappedData, mmapFilesize);
81 assert(rc == 0);
82 (void)rc;
83 close(mmapFD);
86 bool ConstFieldsRewrite::VisitFieldDecl(const FieldDecl* fieldDecl)
88 if (ignoreLocation(fieldDecl))
89 return true;
90 // ignore stuff that forms part of the stable URE interface
91 if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(
92 fieldDecl->getCanonicalDecl()->getLocation())))
93 return true;
94 // in case we've already processed this field
95 if (fieldDecl->getType().isConstQualified())
96 return true;
97 // TODO rewriting T& is a bit trickier
98 if (loplugin::TypeCheck(fieldDecl->getType()).LvalueReference())
99 return true;
101 const RecordDecl* recordDecl = fieldDecl->getParent();
102 std::string parentClassName;
103 if (const CXXRecordDecl* cxxRecordDecl = dyn_cast<CXXRecordDecl>(recordDecl))
105 if (cxxRecordDecl->getTemplateInstantiationPattern())
106 cxxRecordDecl = cxxRecordDecl->getTemplateInstantiationPattern();
107 parentClassName = cxxRecordDecl->getQualifiedNameAsString();
109 else
111 parentClassName = recordDecl->getQualifiedNameAsString();
113 // the extra spaces match the formatting in the results file, and help avoid false+
114 std::string aNiceName = " " + parentClassName + " " + fieldDecl->getNameAsString() + " "
115 + fieldDecl->getType().getAsString() + "\n";
117 // search mmap'ed file for field
118 const char* aNiceNameStr = aNiceName.c_str();
119 char* found = std::search(mmappedData, mmappedData + mmapFilesize, aNiceNameStr,
120 aNiceNameStr + aNiceName.size());
121 if (!(found < mmappedData + mmapFilesize))
122 return true;
124 SourceManager& SM = compiler.getSourceManager();
125 auto endLoc = fieldDecl->getTypeSourceInfo()->getTypeLoc().getEndLoc();
126 endLoc = endLoc.getLocWithOffset(Lexer::MeasureTokenLength(endLoc, SM, compiler.getLangOpts()));
128 // Calculate how much space is available after the type declaration that we can use to
129 // overwrite with the " const". This reduces the amount of formatting fixups I need to do.
130 char const* p1 = SM.getCharacterData(endLoc);
131 bool success = false;
132 if (*p1 != ' ')
134 // Sometimes there is no space at all e.g. in
135 // FastTokenHandlerBase *mpTokenHandler;
136 // between the "*" and the "mpTokenHandler", so add an extra space.
137 success = insertText(endLoc, " const ");
139 else
141 int spaceAvailable = 1;
142 ++p1;
143 for (; spaceAvailable < 6; ++spaceAvailable)
145 if (*p1 != ' ')
146 break;
147 ++p1;
149 if (spaceAvailable < 6)
150 success = replaceText(endLoc, spaceAvailable - 1, " const");
151 else
152 success = replaceText(endLoc, spaceAvailable, " const");
155 if (!success)
157 report(DiagnosticsEngine::Warning, "Could not mark field as const",
158 fieldDecl->getBeginLoc())
159 << fieldDecl->getSourceRange();
161 return true;
164 loplugin::Plugin::Registration<ConstFieldsRewrite> X("constfieldsrewrite", false);
167 #endif
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */