bump product version to 6.4.0.3
[LibreOffice.git] / compilerplugins / clang / constfieldsrewrite.cxx
blob03fb3d0c36093044e1ecb054759fd50ce2923729
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 <sys/mman.h>
18 #include <sys/types.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <sys/stat.h>
22 #include <assert.h>
23 #include <cstring>
25 /**
26 This is intended to be run as the second stage of the "constfields" clang plugin.
29 namespace
31 class ConstFieldsRewrite : public RecursiveASTVisitor<ConstFieldsRewrite>,
32 public loplugin::RewritePlugin
34 public:
35 explicit ConstFieldsRewrite(loplugin::InstantiationData const& data);
36 ~ConstFieldsRewrite();
38 virtual void run() override
40 if (rewriter)
42 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
46 bool VisitFieldDecl(const FieldDecl* var);
48 private:
49 // I use a brute-force approach - mmap the results file and do a linear search on it
50 // It works surprisingly well, because the file is small enough to fit into L2 cache on modern CPU's
51 size_t mmapFilesize;
52 int mmapFD;
53 char* mmappedData;
56 size_t getFilesize(const char* filename)
58 struct stat st;
59 stat(filename, &st);
60 return st.st_size;
63 ConstFieldsRewrite::ConstFieldsRewrite(loplugin::InstantiationData const& data)
64 : RewritePlugin(data)
66 static const char sInputFile[] = SRCDIR "/compilerplugins/clang/constfields.results";
67 mmapFilesize = getFilesize(sInputFile);
68 //Open file
69 mmapFD = open(sInputFile, O_RDONLY, 0);
70 assert(mmapFD != -1);
71 //Execute mmap
72 mmappedData = static_cast<char*>(mmap(NULL, mmapFilesize, PROT_READ, MAP_PRIVATE, mmapFD, 0));
73 assert(mmappedData != NULL);
76 ConstFieldsRewrite::~ConstFieldsRewrite()
78 //Cleanup
79 int rc = munmap(mmappedData, mmapFilesize);
80 assert(rc == 0);
81 (void)rc;
82 close(mmapFD);
85 bool ConstFieldsRewrite::VisitFieldDecl(const FieldDecl* fieldDecl)
87 if (ignoreLocation(fieldDecl))
88 return true;
89 // ignore stuff that forms part of the stable URE interface
90 if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(
91 fieldDecl->getCanonicalDecl()->getLocation())))
92 return true;
93 // in case we've already processed this field
94 if (fieldDecl->getType().isConstQualified())
95 return true;
96 // in case we've already processed this field
97 if (fieldDecl->getType().isConstQualified())
98 return true;
99 // TODO rewriting T& is a bit trickier
100 if (loplugin::TypeCheck(fieldDecl->getType()).LvalueReference())
101 return true;
103 const RecordDecl* recordDecl = fieldDecl->getParent();
104 std::string parentClassName;
105 if (const CXXRecordDecl* cxxRecordDecl = dyn_cast<CXXRecordDecl>(recordDecl))
107 if (cxxRecordDecl->getTemplateInstantiationPattern())
108 cxxRecordDecl = cxxRecordDecl->getTemplateInstantiationPattern();
109 parentClassName = cxxRecordDecl->getQualifiedNameAsString();
111 else
113 parentClassName = recordDecl->getQualifiedNameAsString();
115 // the extra spaces match the formatting in the results file, and help avoid false+
116 std::string aNiceName = " " + parentClassName + " " + fieldDecl->getNameAsString() + " "
117 + fieldDecl->getType().getAsString() + "\n";
119 // search mmap'ed file for field
120 const char* aNiceNameStr = aNiceName.c_str();
121 char* found = std::search(mmappedData, mmappedData + mmapFilesize, aNiceNameStr,
122 aNiceNameStr + aNiceName.size());
123 if (!(found < mmappedData + mmapFilesize))
124 return true;
126 SourceManager& SM = compiler.getSourceManager();
127 auto endLoc = fieldDecl->getTypeSourceInfo()->getTypeLoc().getEndLoc();
128 endLoc = endLoc.getLocWithOffset(Lexer::MeasureTokenLength(endLoc, SM, compiler.getLangOpts()));
130 // Calculate how much space is available after the type declaration that we can use to
131 // overwrite with the " const". This reduces the amount of formatting fixups I need to do.
132 char const* p1 = SM.getCharacterData(endLoc);
133 bool success = false;
134 if (*p1 != ' ')
136 // Sometimes there is no space at all e.g. in
137 // FastTokenHandlerBase *mpTokenHandler;
138 // between the "*" and the "mpTokenHandler", so add an extra space.
139 success = insertText(endLoc, " const ");
141 else
143 int spaceAvailable = 1;
144 ++p1;
145 for (; spaceAvailable < 6; ++spaceAvailable)
147 if (*p1 != ' ')
148 break;
149 ++p1;
151 if (spaceAvailable < 6)
152 success = replaceText(endLoc, spaceAvailable - 1, " const");
153 else
154 success = replaceText(endLoc, spaceAvailable, " const");
157 if (!success)
159 report(DiagnosticsEngine::Warning, "Could not mark field as const",
160 compat::getBeginLoc(fieldDecl))
161 << fieldDecl->getSourceRange();
163 return true;
166 loplugin::Plugin::Registration<ConstFieldsRewrite> X("constfieldsrewrite", false);
169 #endif
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */