bump product version to 7.2.5.1
[LibreOffice.git] / compilerplugins / clang / constantparam.cxx
blob71c0f69da61a2eb2f52a4319bbc12c1212adc6f9
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 #include <string>
11 #include <set>
12 #include <iostream>
13 #include <fstream>
15 #include "plugin.hxx"
16 #include "compat.hxx"
17 #include "check.hxx"
18 #include "functionaddress.hxx"
21 Find params on methods where the param is only ever passed as a single constant value.
23 The process goes something like this:
24 $ make check
25 $ make FORCE_COMPILE_ALL=1 COMPILER_PLUGIN_TOOL='constantparam' check
26 $ ./compilerplugins/clang/constantparam.py
28 TODO look for OUString and OString params and check for call-params that are always either "" or default constructed
30 FIXME this plugin manages to trigger crashes inside clang, when calling EvaluateAsInt, so I end up disabling it for a handful of files
31 here and there.
34 namespace {
36 struct MyCallSiteInfo
38 std::string returnType;
39 std::string nameAndParams;
40 std::string paramName;
41 std::string paramType;
42 int paramIndex; // because in some declarations the names are empty
43 std::string callValue;
44 std::string sourceLocation;
46 bool operator < (const MyCallSiteInfo &lhs, const MyCallSiteInfo &rhs)
48 return std::tie(lhs.sourceLocation, lhs.paramIndex, lhs.callValue)
49 < std::tie(rhs.sourceLocation, rhs.paramIndex, rhs.callValue);
53 // try to limit the voluminous output a little
54 static std::set<MyCallSiteInfo> callSet;
56 class ConstantParam:
57 public loplugin::FunctionAddress<loplugin::FilteringPlugin<ConstantParam>>
59 public:
60 explicit ConstantParam(loplugin::InstantiationData const & data): FunctionAddress(data) {}
62 virtual void run() override
64 // ignore some files that make clang crash inside EvaluateAsInt
65 std::string fn(handler.getMainFileName());
66 loplugin::normalizeDotDotInFilePath(fn);
67 if (loplugin::isSamePathname(fn, SRCDIR "/basegfx/source/matrix/b2dhommatrix.cxx")
68 || loplugin::isSamePathname(fn, SRCDIR "/basegfx/source/matrix/b3dhommatrix.cxx"))
69 return;
71 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
73 // this catches places that take the address of a method
74 for (auto functionDecl : getFunctionsWithAddressTaken())
76 for (unsigned i = 0; i < functionDecl->getNumParams(); ++i)
77 addToCallSet(functionDecl, i, functionDecl->getParamDecl(i)->getName(), "unknown3");
80 // dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes
81 // writing to the same logfile
83 std::string output;
84 for (const MyCallSiteInfo & s : callSet)
85 output += s.returnType + "\t" + s.nameAndParams + "\t" + s.sourceLocation + "\t"
86 + s.paramName + "\t" + s.paramType + "\t" + s.callValue + "\n";
87 std::ofstream myfile;
88 myfile.open( WORKDIR "/loplugin.constantparam.log", std::ios::app | std::ios::out);
89 myfile << output;
90 myfile.close();
93 bool shouldVisitTemplateInstantiations () const { return true; }
94 bool shouldVisitImplicitCode () const { return true; }
96 bool VisitCallExpr( const CallExpr* );
97 bool VisitCXXConstructExpr( const CXXConstructExpr* );
98 private:
99 void addToCallSet(const FunctionDecl* functionDecl, int paramIndex, llvm::StringRef paramName, const std::string& callValue);
100 std::string getCallValue(const Expr* arg);
103 void ConstantParam::addToCallSet(const FunctionDecl* functionDecl, int paramIndex, llvm::StringRef paramName, const std::string& callValue)
105 if (functionDecl->getInstantiatedFromMemberFunction())
106 functionDecl = functionDecl->getInstantiatedFromMemberFunction();
107 #if CLANG_VERSION < 90000
108 else if (functionDecl->getClassScopeSpecializationPattern())
109 functionDecl = functionDecl->getClassScopeSpecializationPattern();
110 #endif
111 else if (functionDecl->getTemplateInstantiationPattern())
112 functionDecl = functionDecl->getTemplateInstantiationPattern();
114 if (!functionDecl->getNameInfo().getLoc().isValid())
115 return;
116 if (functionDecl->isVariadic())
117 return;
118 if (ignoreLocation(functionDecl))
119 return;
120 // ignore stuff that forms part of the stable URE interface
121 if (isInUnoIncludeFile(functionDecl))
122 return;
123 SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( functionDecl->getLocation() );
124 StringRef filename = getFilenameOfLocation(expansionLoc);
125 if (!loplugin::hasPathnamePrefix(filename, SRCDIR "/"))
126 return;
127 filename = filename.substr(strlen(SRCDIR)+1);
130 MyCallSiteInfo aInfo;
131 aInfo.returnType = functionDecl->getReturnType().getCanonicalType().getAsString();
133 if (isa<CXXMethodDecl>(functionDecl)) {
134 const CXXRecordDecl* recordDecl = dyn_cast<CXXMethodDecl>(functionDecl)->getParent();
135 aInfo.nameAndParams += recordDecl->getQualifiedNameAsString();
136 aInfo.nameAndParams += "::";
138 aInfo.nameAndParams += functionDecl->getNameAsString() + "(";
139 bool bFirst = true;
140 for (const ParmVarDecl *pParmVarDecl : functionDecl->parameters()) {
141 if (bFirst)
142 bFirst = false;
143 else
144 aInfo.nameAndParams += ",";
145 aInfo.nameAndParams += pParmVarDecl->getType().getCanonicalType().getAsString();
147 aInfo.nameAndParams += ")";
148 if (isa<CXXMethodDecl>(functionDecl) && dyn_cast<CXXMethodDecl>(functionDecl)->isConst()) {
149 aInfo.nameAndParams += " const";
151 aInfo.paramName = paramName.str();
152 aInfo.paramIndex = paramIndex;
153 if (paramIndex < (int)functionDecl->getNumParams())
154 aInfo.paramType = functionDecl->getParamDecl(paramIndex)->getType().getCanonicalType().getAsString();
155 aInfo.callValue = callValue;
157 aInfo.sourceLocation = filename.str() + ":" + std::to_string(compiler.getSourceManager().getSpellingLineNumber(expansionLoc));
158 loplugin::normalizeDotDotInFilePath(aInfo.sourceLocation);
160 callSet.insert(aInfo);
163 std::string ConstantParam::getCallValue(const Expr* arg)
165 arg = arg->IgnoreParenCasts();
166 if (isa<CXXDefaultArgExpr>(arg)) {
167 arg = dyn_cast<CXXDefaultArgExpr>(arg)->getExpr();
169 arg = arg->IgnoreParenCasts();
170 // ignore this, it seems to trigger an infinite recursion
171 if (isa<UnaryExprOrTypeTraitExpr>(arg))
172 return "unknown1";
173 if (arg->isValueDependent())
174 return "unknown2";
175 APSInt x1;
176 if (compat::EvaluateAsInt(arg, x1, compiler.getASTContext()))
178 return x1.toString(10);
180 if (isa<CXXNullPtrLiteralExpr>(arg)) {
181 return "0";
183 if (isa<MaterializeTemporaryExpr>(arg))
185 const CXXBindTemporaryExpr* strippedArg = dyn_cast_or_null<CXXBindTemporaryExpr>(arg->IgnoreParenCasts());
186 if (strippedArg)
188 auto temp = dyn_cast<CXXTemporaryObjectExpr>(strippedArg->getSubExpr());
189 if (temp->getNumArgs() == 0)
191 if (loplugin::TypeCheck(temp->getType()).Class("OUString").Namespace("rtl").GlobalNamespace()) {
192 return "\"\"";
194 if (loplugin::TypeCheck(temp->getType()).Class("OString").Namespace("rtl").GlobalNamespace()) {
195 return "\"\"";
197 return "defaultConstruct";
202 // Get the expression contents.
203 // This helps us find params which are always initialised with something like "OUString()".
204 SourceManager& SM = compiler.getSourceManager();
205 SourceLocation startLoc = compat::getBeginLoc(arg);
206 SourceLocation endLoc = compat::getEndLoc(arg);
207 const char *p1 = SM.getCharacterData( startLoc );
208 const char *p2 = SM.getCharacterData( endLoc );
209 if (!p1 || !p2 || (p2 - p1) < 0 || (p2 - p1) > 40) {
210 return "unknown";
212 unsigned n = Lexer::MeasureTokenLength( endLoc, SM, compiler.getLangOpts());
213 std::string s( p1, p2 - p1 + n);
214 // strip linefeed and tab characters so they don't interfere with the parsing of the log file
215 std::replace( s.begin(), s.end(), '\r', ' ');
216 std::replace( s.begin(), s.end(), '\n', ' ');
217 std::replace( s.begin(), s.end(), '\t', ' ');
219 // now normalize the value. For some params, like OUString, we can pass it as OUString() or "" and they are the same thing
220 if (s == "OUString()")
221 s = "\"\"";
222 else if (s == "OString()")
223 s = "\"\"";
224 else if (s == "aEmptyOUStr") //sw
225 s = "\"\"";
226 else if (s == "EMPTY_OUSTRING")//sc
227 s = "\"\"";
228 else if (s == "GetEmptyOUString()") //sc
229 s = "\"\"";
230 return s;
233 bool ConstantParam::VisitCallExpr(const CallExpr * callExpr) {
234 if (ignoreLocation(callExpr)) {
235 return true;
237 const FunctionDecl* functionDecl;
238 if (isa<CXXMemberCallExpr>(callExpr)) {
239 functionDecl = dyn_cast<CXXMemberCallExpr>(callExpr)->getMethodDecl();
241 else {
242 functionDecl = callExpr->getDirectCallee();
244 if (!functionDecl)
245 return true;
246 functionDecl = functionDecl->getCanonicalDecl();
247 // method overrides don't always specify the same default params (although they probably should)
248 // so we need to work our way up to the root method
249 while (isa<CXXMethodDecl>(functionDecl)) {
250 const CXXMethodDecl* methodDecl = dyn_cast<CXXMethodDecl>(functionDecl);
251 if (methodDecl->size_overridden_methods()==0)
252 break;
253 functionDecl = *methodDecl->begin_overridden_methods();
255 // work our way back to the root definition for template methods
256 if (functionDecl->getInstantiatedFromMemberFunction())
257 functionDecl = functionDecl->getInstantiatedFromMemberFunction();
258 #if CLANG_VERSION < 90000
259 else if (functionDecl->getClassScopeSpecializationPattern())
260 functionDecl = functionDecl->getClassScopeSpecializationPattern();
261 #endif
262 else if (functionDecl->getTemplateInstantiationPattern())
263 functionDecl = functionDecl->getTemplateInstantiationPattern();
265 unsigned len = std::max(callExpr->getNumArgs(), functionDecl->getNumParams());
266 for (unsigned i = 0; i < len; ++i) {
267 const Expr* valExpr;
268 if (i < callExpr->getNumArgs())
269 valExpr = callExpr->getArg(i);
270 else if (i < functionDecl->getNumParams() && functionDecl->getParamDecl(i)->hasDefaultArg())
271 valExpr = functionDecl->getParamDecl(i)->getDefaultArg();
272 else
273 // can happen in template code
274 continue;
275 std::string callValue = getCallValue(valExpr);
276 std::string paramName = i < functionDecl->getNumParams()
277 ? functionDecl->getParamDecl(i)->getName().str()
278 : "###" + std::to_string(i);
279 addToCallSet(functionDecl, i, paramName, callValue);
281 return true;
284 bool ConstantParam::VisitCXXConstructExpr( const CXXConstructExpr* constructExpr )
286 const CXXConstructorDecl* constructorDecl = constructExpr->getConstructor();
287 constructorDecl = constructorDecl->getCanonicalDecl();
289 unsigned len = std::max(constructExpr->getNumArgs(), constructorDecl->getNumParams());
290 for (unsigned i = 0; i < len; ++i) {
291 const Expr* valExpr;
292 if (i < constructExpr->getNumArgs())
293 valExpr = constructExpr->getArg(i);
294 else if (i < constructorDecl->getNumParams() && constructorDecl->getParamDecl(i)->hasDefaultArg())
295 valExpr = constructorDecl->getParamDecl(i)->getDefaultArg();
296 else
297 // can happen in template code
298 continue;
299 std::string callValue = getCallValue(valExpr);
300 std::string paramName = i < constructorDecl->getNumParams()
301 ? constructorDecl->getParamDecl(i)->getName().str()
302 : "###" + std::to_string(i);
303 addToCallSet(constructorDecl, i, paramName, callValue);
305 return true;
309 loplugin::Plugin::Registration< ConstantParam > X("constantparam", false);
313 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */