Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
core
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
LibreOffice
core
Commits
569c7da2
Kaydet (Commit)
569c7da2
authored
Eki 27, 2017
tarafından
Noel Grandin
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
extract some common code for checking if a functions address was taken
Change-Id: I292b4e9bf17c83f83ff43ac4c5870d33092d0c71
üst
5670f65c
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
138 additions
and
106 deletions
+138
-106
constantparam.cxx
compilerplugins/clang/constantparam.cxx
+10
-18
constparams.cxx
compilerplugins/clang/constparams.cxx
+5
-86
functionaddress.hxx
compilerplugins/clang/functionaddress.hxx
+120
-0
plugin.hxx
compilerplugins/clang/plugin.hxx
+3
-2
No files found.
compilerplugins/clang/constantparam.cxx
Dosyayı görüntüle @
569c7da2
...
...
@@ -15,6 +15,7 @@
#include "plugin.hxx"
#include "compat.hxx"
#include "check.hxx"
#include "functionaddress.hxx"
/*
Find params on methods where the param is only ever passed as a single constant value.
...
...
@@ -53,10 +54,10 @@ bool operator < (const MyCallSiteInfo &lhs, const MyCallSiteInfo &rhs)
static
std
::
set
<
MyCallSiteInfo
>
callSet
;
class
ConstantParam
:
public
RecursiveASTVisitor
<
ConstantParam
>
,
public
loplugin
::
Plugin
public
loplugin
::
FunctionAddress
<
ConstantParam
>
{
public
:
explicit
ConstantParam
(
InstantiationData
const
&
data
)
:
Plugin
(
data
)
{}
explicit
ConstantParam
(
InstantiationData
const
&
data
)
:
loplugin
::
FunctionAddress
<
ConstantParam
>
(
data
)
{}
virtual
void
run
()
override
{
...
...
@@ -70,6 +71,13 @@ public:
TraverseDecl
(
compiler
.
getASTContext
().
getTranslationUnitDecl
());
// this catches places that take the address of a method
for
(
auto
functionDecl
:
getFunctionsWithAddressTaken
())
{
for
(
unsigned
i
=
0
;
i
<
functionDecl
->
getNumParams
();
++
i
)
addToCallSet
(
functionDecl
,
i
,
functionDecl
->
getParamDecl
(
i
)
->
getName
(),
"unknown3"
);
}
// dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes
// writing to the same logfile
...
...
@@ -87,7 +95,6 @@ public:
bool
shouldVisitImplicitCode
()
const
{
return
true
;
}
bool
VisitCallExpr
(
const
CallExpr
*
);
bool
VisitDeclRefExpr
(
const
DeclRefExpr
*
);
bool
VisitCXXConstructExpr
(
const
CXXConstructExpr
*
);
private
:
void
addToCallSet
(
const
FunctionDecl
*
functionDecl
,
int
paramIndex
,
llvm
::
StringRef
paramName
,
const
std
::
string
&
callValue
);
...
...
@@ -277,21 +284,6 @@ bool ConstantParam::VisitCallExpr(const CallExpr * callExpr) {
return
true
;
}
// this catches places that take the address of a method
bool
ConstantParam
::
VisitDeclRefExpr
(
const
DeclRefExpr
*
declRefExpr
)
{
const
Decl
*
decl
=
declRefExpr
->
getDecl
();
const
FunctionDecl
*
functionDecl
=
dyn_cast
<
FunctionDecl
>
(
decl
);
if
(
!
functionDecl
)
return
true
;
functionDecl
=
functionDecl
->
getCanonicalDecl
();
for
(
unsigned
i
=
0
;
i
<
functionDecl
->
getNumParams
();
++
i
)
{
addToCallSet
(
functionDecl
,
i
,
functionDecl
->
getParamDecl
(
i
)
->
getName
(),
"unknown3"
);
}
return
true
;
}
bool
ConstantParam
::
VisitCXXConstructExpr
(
const
CXXConstructExpr
*
constructExpr
)
{
const
CXXConstructorDecl
*
constructorDecl
=
constructExpr
->
getConstructor
();
...
...
compilerplugins/clang/constparams.cxx
Dosyayı görüntüle @
569c7da2
...
...
@@ -16,6 +16,7 @@
#include "plugin.hxx"
#include "compat.hxx"
#include "check.hxx"
#include "functionaddress.hxx"
/**
Find pointer and reference params that can be declared const.
...
...
@@ -31,10 +32,10 @@ static bool startswith(const std::string& rStr, const char* pSubStr) {
}
class
ConstParams
:
public
RecursiveASTVisitor
<
ConstParams
>
,
public
loplugin
::
Plugin
public
loplugin
::
FunctionAddress
<
ConstParams
>
{
public
:
explicit
ConstParams
(
InstantiationData
const
&
data
)
:
Plugin
(
data
)
{}
explicit
ConstParams
(
InstantiationData
const
&
data
)
:
loplugin
::
FunctionAddress
<
ConstParams
>
(
data
)
{}
virtual
void
run
()
override
{
std
::
string
fn
(
compiler
.
getSourceManager
().
getFileEntryForID
(
...
...
@@ -63,8 +64,8 @@ public:
if
(
paramCannotBeConstSet
.
find
(
pParmVarDecl
)
==
paramCannotBeConstSet
.
end
())
{
auto
functionDecl
=
parmToFunction
[
pParmVarDecl
];
auto
canonicalDecl
=
functionDecl
->
getCanonicalDecl
();
if
(
ignoredFunctions_
.
find
(
canonicalDecl
)
!=
ignoredFunctions_
.
end
())
if
(
getFunctionsWithAddressTaken
()
.
find
(
canonicalDecl
)
!=
getFunctionsWithAddressTaken
()
.
end
())
{
continue
;
}
...
...
@@ -86,86 +87,6 @@ public:
}
}
bool
TraverseCallExpr
(
CallExpr
*
expr
)
{
auto
const
saved
=
callee_
;
callee_
=
expr
->
getCallee
();
auto
const
ret
=
RecursiveASTVisitor
::
TraverseCallExpr
(
expr
);
callee_
=
saved
;
return
ret
;
}
bool
TraverseCXXOperatorCallExpr
(
CXXOperatorCallExpr
*
expr
)
{
auto
const
saved
=
callee_
;
callee_
=
expr
->
getCallee
();
auto
const
ret
=
RecursiveASTVisitor
::
TraverseCXXOperatorCallExpr
(
expr
);
callee_
=
saved
;
return
ret
;
}
bool
TraverseCXXMemberCallExpr
(
CXXMemberCallExpr
*
expr
)
{
auto
const
saved
=
callee_
;
callee_
=
expr
->
getCallee
();
auto
const
ret
=
RecursiveASTVisitor
::
TraverseCXXMemberCallExpr
(
expr
);
callee_
=
saved
;
return
ret
;
}
bool
TraverseCUDAKernelCallExpr
(
CUDAKernelCallExpr
*
expr
)
{
auto
const
saved
=
callee_
;
callee_
=
expr
->
getCallee
();
auto
const
ret
=
RecursiveASTVisitor
::
TraverseCUDAKernelCallExpr
(
expr
);
callee_
=
saved
;
return
ret
;
}
bool
TraverseUserDefinedLiteral
(
UserDefinedLiteral
*
expr
)
{
auto
const
saved
=
callee_
;
callee_
=
expr
->
getCallee
();
auto
const
ret
=
RecursiveASTVisitor
::
TraverseUserDefinedLiteral
(
expr
);
callee_
=
saved
;
return
ret
;
}
bool
VisitImplicitCastExpr
(
ImplicitCastExpr
const
*
expr
)
{
if
(
expr
==
callee_
)
{
return
true
;
}
if
(
ignoreLocation
(
expr
))
{
return
true
;
}
if
(
expr
->
getCastKind
()
!=
CK_FunctionToPointerDecay
)
{
return
true
;
}
auto
const
dre
=
dyn_cast
<
DeclRefExpr
>
(
expr
->
getSubExpr
()
->
IgnoreParens
());
if
(
dre
==
nullptr
)
{
return
true
;
}
auto
const
fd
=
dyn_cast
<
FunctionDecl
>
(
dre
->
getDecl
());
if
(
fd
==
nullptr
)
{
return
true
;
}
ignoredFunctions_
.
insert
(
fd
->
getCanonicalDecl
());
return
true
;
}
bool
VisitUnaryAddrOf
(
UnaryOperator
const
*
expr
)
{
if
(
ignoreLocation
(
expr
))
{
return
true
;
}
auto
const
dre
=
dyn_cast
<
DeclRefExpr
>
(
expr
->
getSubExpr
()
->
IgnoreParenImpCasts
());
if
(
dre
==
nullptr
)
{
return
true
;
}
auto
const
fd
=
dyn_cast
<
FunctionDecl
>
(
dre
->
getDecl
());
if
(
fd
==
nullptr
)
{
return
true
;
}
ignoredFunctions_
.
insert
(
fd
->
getCanonicalDecl
());
return
true
;
}
bool
VisitFunctionDecl
(
const
FunctionDecl
*
);
bool
VisitDeclRefExpr
(
const
DeclRefExpr
*
);
...
...
@@ -176,8 +97,6 @@ private:
std
::
unordered_set
<
const
ParmVarDecl
*>
interestingParamSet
;
std
::
unordered_map
<
const
ParmVarDecl
*
,
const
FunctionDecl
*>
parmToFunction
;
std
::
unordered_set
<
const
ParmVarDecl
*>
paramCannotBeConstSet
;
std
::
unordered_set
<
FunctionDecl
const
*>
ignoredFunctions_
;
Expr
const
*
callee_
=
nullptr
;
};
bool
ConstParams
::
VisitFunctionDecl
(
const
FunctionDecl
*
functionDecl
)
...
...
compilerplugins/clang/functionaddress.hxx
0 → 100644
Dosyayı görüntüle @
569c7da2
/* -*- 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/.
*/
#ifndef INCLUDED_COMPILERPLUGINS_CLANG_FUNCTIONADDRESS_HXX
#define INCLUDED_COMPILERPLUGINS_CLANG_FUNCTIONADDRESS_HXX
#include <clang/AST/RecursiveASTVisitor.h>
#include <unordered_set>
#include "plugin.hxx"
/**
* Common code for checking if the address of a function was taken.
*/
namespace
loplugin
{
template
<
typename
Derived
>
class
FunctionAddress
:
public
RecursiveASTVisitor
<
Derived
>
,
public
loplugin
::
Plugin
{
public
:
explicit
FunctionAddress
(
const
InstantiationData
&
data
)
:
loplugin
::
Plugin
(
data
)
{}
bool
TraverseCallExpr
(
CallExpr
*
expr
)
{
auto
const
saved
=
callee_
;
callee_
=
expr
->
getCallee
();
auto
const
ret
=
RecursiveASTVisitor
<
Derived
>::
TraverseCallExpr
(
expr
);
callee_
=
saved
;
return
ret
;
}
bool
TraverseCXXOperatorCallExpr
(
CXXOperatorCallExpr
*
expr
)
{
auto
const
saved
=
callee_
;
callee_
=
expr
->
getCallee
();
auto
const
ret
=
RecursiveASTVisitor
<
Derived
>::
TraverseCXXOperatorCallExpr
(
expr
);
callee_
=
saved
;
return
ret
;
}
bool
TraverseCXXMemberCallExpr
(
CXXMemberCallExpr
*
expr
)
{
auto
const
saved
=
callee_
;
callee_
=
expr
->
getCallee
();
auto
const
ret
=
RecursiveASTVisitor
<
Derived
>::
TraverseCXXMemberCallExpr
(
expr
);
callee_
=
saved
;
return
ret
;
}
bool
TraverseCUDAKernelCallExpr
(
CUDAKernelCallExpr
*
expr
)
{
auto
const
saved
=
callee_
;
callee_
=
expr
->
getCallee
();
auto
const
ret
=
RecursiveASTVisitor
<
Derived
>::
TraverseCUDAKernelCallExpr
(
expr
);
callee_
=
saved
;
return
ret
;
}
bool
TraverseUserDefinedLiteral
(
UserDefinedLiteral
*
expr
)
{
auto
const
saved
=
callee_
;
callee_
=
expr
->
getCallee
();
auto
const
ret
=
RecursiveASTVisitor
<
Derived
>::
TraverseUserDefinedLiteral
(
expr
);
callee_
=
saved
;
return
ret
;
}
bool
VisitImplicitCastExpr
(
ImplicitCastExpr
const
*
expr
)
{
if
(
expr
==
callee_
)
{
return
true
;
}
if
(
ignoreLocation
(
expr
))
{
return
true
;
}
if
(
expr
->
getCastKind
()
!=
CK_FunctionToPointerDecay
)
{
return
true
;
}
auto
const
dre
=
dyn_cast
<
DeclRefExpr
>
(
expr
->
getSubExpr
()
->
IgnoreParens
());
if
(
dre
==
nullptr
)
{
return
true
;
}
auto
const
fd
=
dyn_cast
<
FunctionDecl
>
(
dre
->
getDecl
());
if
(
fd
==
nullptr
)
{
return
true
;
}
ignoredFunctions_
.
insert
(
fd
->
getCanonicalDecl
());
return
true
;
}
bool
VisitUnaryAddrOf
(
UnaryOperator
const
*
expr
)
{
if
(
ignoreLocation
(
expr
))
{
return
true
;
}
auto
const
dre
=
dyn_cast
<
DeclRefExpr
>
(
expr
->
getSubExpr
()
->
IgnoreParenImpCasts
());
if
(
dre
==
nullptr
)
{
return
true
;
}
auto
const
fd
=
dyn_cast
<
FunctionDecl
>
(
dre
->
getDecl
());
if
(
fd
==
nullptr
)
{
return
true
;
}
ignoredFunctions_
.
insert
(
fd
->
getCanonicalDecl
());
return
true
;
}
protected
:
std
::
unordered_set
<
FunctionDecl
const
*>
const
&
getFunctionsWithAddressTaken
()
{
return
ignoredFunctions_
;
}
private
:
std
::
unordered_set
<
FunctionDecl
const
*>
ignoredFunctions_
;
Expr
const
*
callee_
=
nullptr
;
};
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
compilerplugins/clang/plugin.hxx
Dosyayı görüntüle @
569c7da2
...
...
@@ -56,11 +56,12 @@ public:
enum
{
isPPCallback
=
false
};
// Returns location right after the end of the token that starts at the given location.
SourceLocation
locationAfterToken
(
SourceLocation
location
);
protected
:
DiagnosticBuilder
report
(
DiagnosticsEngine
::
Level
level
,
StringRef
message
,
SourceLocation
loc
=
SourceLocation
())
const
;
bool
ignoreLocation
(
SourceLocation
loc
);
bool
ignoreLocation
(
const
Decl
*
decl
);
bool
ignoreLocation
(
const
Stmt
*
stmt
);
protected
:
DiagnosticBuilder
report
(
DiagnosticsEngine
::
Level
level
,
StringRef
message
,
SourceLocation
loc
=
SourceLocation
())
const
;
CompilerInstance
&
compiler
;
PluginHandler
&
handler
;
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment