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
81b58bb0
Kaydet (Commit)
81b58bb0
authored
Haz 19, 2013
tarafından
Luboš Luňák
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
simplify postfixincrementfix plugin using parentStmt()
Change-Id: I93fa422afe7f3e1e10576dd64af9d57b2302f44e
üst
ade47d3d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
67 deletions
+27
-67
postfixincrementfix.cxx
compilerplugins/clang/postfixincrementfix.cxx
+23
-60
postfixincrementfix.hxx
compilerplugins/clang/postfixincrementfix.hxx
+4
-7
No files found.
compilerplugins/clang/postfixincrementfix.cxx
Dosyayı görüntüle @
81b58bb0
...
...
@@ -29,40 +29,20 @@ void PostfixIncrementFix::run()
TraverseDecl
(
compiler
.
getASTContext
().
getTranslationUnitDecl
());
}
bool
PostfixIncrementFix
::
Visit
FunctionDecl
(
const
FunctionDecl
*
declaration
)
bool
PostfixIncrementFix
::
Visit
CXXOperatorCallExpr
(
const
CXXOperatorCallExpr
*
op
)
{
if
(
ignoreLocation
(
declaration
))
if
(
ignoreLocation
(
op
))
return
true
;
if
(
!
declaration
->
doesThisDeclarationHaveABody
())
return
true
;
StmtParents
parents
;
fixPostfixOperators
(
declaration
->
getBody
(),
parents
);
return
true
;
}
void
PostfixIncrementFix
::
fixPostfixOperators
(
const
Stmt
*
stmt
,
StmtParents
&
parents
)
{
if
(
const
CXXOperatorCallExpr
*
op
=
dyn_cast
<
CXXOperatorCallExpr
>
(
stmt
))
{
// postfix ++ has two arguments (the operand and the hidden extra int)
if
(
op
->
getOperator
()
==
OO_PlusPlus
&&
op
->
getNumArgs
()
==
2
)
fixPostfixOperator
(
op
,
parents
);
}
// postfix ++ has two arguments (the operand and the hidden extra int)
if
(
op
->
getOperator
()
==
OO_PlusPlus
&&
op
->
getNumArgs
()
==
2
)
fixPostfixOperator
(
op
);
// For primitive types it would be UnaryOperatorExpr, but probably no good reason to change those.
parents
.
push_back
(
stmt
);
for
(
ConstStmtIterator
it
=
stmt
->
child_begin
();
it
!=
stmt
->
child_end
();
++
it
)
{
if
(
*
it
!=
NULL
)
// some children can be apparently NULL
fixPostfixOperators
(
*
it
,
parents
);
}
assert
(
parents
.
back
()
==
stmt
);
parents
.
pop_back
();
return
true
;
}
void
PostfixIncrementFix
::
fixPostfixOperator
(
const
CXXOperatorCallExpr
*
op
,
StmtParents
&
parents
)
void
PostfixIncrementFix
::
fixPostfixOperator
(
const
CXXOperatorCallExpr
*
op
)
{
if
(
!
canChangePostfixToPrefix
(
op
,
parents
,
parents
.
size
()
-
1
))
if
(
!
canChangePostfixToPrefix
(
op
,
op
))
return
;
if
(
!
shouldDoChange
(
op
->
getArg
(
0
)))
return
;
...
...
@@ -73,10 +53,13 @@ void PostfixIncrementFix::fixPostfixOperator( const CXXOperatorCallExpr* op, Stm
removeText
(
op
->
getCallee
()
->
getSourceRange
());
}
bool
PostfixIncrementFix
::
canChangePostfixToPrefix
(
const
CXXOperatorCallExpr
*
op
,
StmtParents
&
parents
,
int
parent_pos
)
bool
PostfixIncrementFix
::
canChangePostfixToPrefix
(
const
Stmt
*
stmt
,
const
CXXOperatorCallExpr
*
op
)
{
const
Stmt
*
parent
=
parentStmt
(
stmt
);
if
(
parent
==
NULL
)
return
true
;
// check if foo++ can be safely replaced by ++foo
switch
(
parent
s
[
parent_pos
]
->
getStmtClass
())
switch
(
parent
->
getStmtClass
())
{
case
Stmt
:
:
CompoundStmtClass
:
return
true
;
...
...
@@ -91,52 +74,32 @@ bool PostfixIncrementFix::canChangePostfixToPrefix( const CXXOperatorCallExpr* o
case
Stmt
:
:
CXXBindTemporaryExprClass
:
// tricky, it may just mean the temporary will be cleaned up
// (by ExprWithCleanups), ignore and go up
assert
(
parent_pos
>
0
);
// should not happen
return
canChangePostfixToPrefix
(
op
,
parents
,
parent_pos
-
1
);
return
canChangePostfixToPrefix
(
parent
,
op
);
case
Stmt
:
:
ExprWithCleanupsClass
:
// cleanup of a temporary, should be harmless (if the use
// of the postfix ++ operator here relies on the fact that
// the dtor for the object will be called, that's pretty insane
// code). Ignore and go up.
assert
(
parent_pos
>
0
);
// should not happen
return
canChangePostfixToPrefix
(
op
,
parents
,
parent_pos
-
1
);
return
canChangePostfixToPrefix
(
parent
,
op
);
case
Stmt
:
:
ParenExprClass
:
// parentheses, go up
assert
(
parent_pos
>
0
);
return
canChangePostfixToPrefix
(
op
,
parents
,
parent_pos
-
1
);
return
canChangePostfixToPrefix
(
parent
,
op
);
case
Stmt
:
:
IfStmtClass
:
return
canChangeInConditionStatement
(
op
,
cast
<
IfStmt
>
(
parents
[
parent_pos
]
)
->
getCond
(),
parents
,
parent_pos
)
;
// cannot be changed in condition, can be changed in statements
return
cast
<
IfStmt
>
(
parent
)
->
getCond
()
!=
stmt
;
case
Stmt
:
:
WhileStmtClass
:
return
canChangeInConditionStatement
(
op
,
cast
<
WhileStmt
>
(
parents
[
parent_pos
]
)
->
getCond
(),
parents
,
parent_pos
);
return
cast
<
WhileStmt
>
(
parent
)
->
getCond
()
!=
stmt
;
case
Stmt
:
:
DoStmtClass
:
return
canChangeInConditionStatement
(
op
,
cast
<
DoStmt
>
(
parents
[
parent_pos
]
)
->
getCond
(),
parents
,
parent_pos
);
return
cast
<
DoStmt
>
(
parent
)
->
getCond
()
!=
stmt
;
case
Stmt
:
:
ForStmtClass
:
return
canChangeInConditionStatement
(
op
,
dyn_cast
<
ForStmt
>
(
parents
[
parent_pos
]
)
->
getCond
(),
parents
,
parent_pos
);
return
cast
<
ForStmt
>
(
parent
)
->
getCond
()
!=
stmt
;
default
:
report
(
DiagnosticsEngine
::
Fatal
,
"cannot analyze operator++ (plugin needs fixing)"
,
op
->
getLocStart
())
<<
parents
[
parent_pos
]
->
getSourceRange
();
// parents[ parent_pos ]->dump();
// parents[ std::max( parent_pos - 3, 0 ) ]->dump();
op
->
getLocStart
())
<<
parent
->
getSourceRange
();
parent
->
dump
();
return
false
;
}
}
bool
PostfixIncrementFix
::
canChangeInConditionStatement
(
const
CXXOperatorCallExpr
*
op
,
const
Expr
*
condition
,
const
StmtParents
&
parents
,
unsigned
int
parent_pos
)
{
// cannot be changed in condition, can be changed in statements
if
(
parent_pos
==
parents
.
size
()
-
1
)
return
op
!=
condition
;
else
{
// indirect child
assert
(
parent_pos
+
1
<
parents
.
size
());
return
parents
[
parent_pos
+
1
]
!=
condition
;
}
}
bool
PostfixIncrementFix
::
shouldDoChange
(
const
Expr
*
operand
)
{
// TODO Changing 'a->b++' to '++a->b' is technically the same, but the latter probably looks confusing,
...
...
compilerplugins/clang/postfixincrementfix.hxx
Dosyayı görüntüle @
81b58bb0
...
...
@@ -23,14 +23,11 @@ class PostfixIncrementFix
public
:
explicit
PostfixIncrementFix
(
CompilerInstance
&
compiler
,
Rewriter
&
rewriter
);
virtual
void
run
()
override
;
bool
Visit
FunctionDecl
(
const
FunctionDecl
*
declaration
);
bool
Visit
CXXOperatorCallExpr
(
const
CXXOperatorCallExpr
*
op
);
private
:
typedef
std
::
vector
<
const
Stmt
*
>
StmtParents
;
void
fixPostfixOperator
(
const
CXXOperatorCallExpr
*
op
,
StmtParents
&
parents
);
void
fixPostfixOperators
(
const
Stmt
*
stmt
,
StmtParents
&
parents
);
bool
canChangePostfixToPrefix
(
const
CXXOperatorCallExpr
*
op
,
StmtParents
&
parents
,
int
parent_pos
);
bool
canChangeInConditionStatement
(
const
CXXOperatorCallExpr
*
op
,
const
Expr
*
condition
,
const
StmtParents
&
parents
,
unsigned
int
parent_pos
);
void
fixPostfixOperator
(
const
CXXOperatorCallExpr
*
op
);
void
fixPostfixOperators
(
const
Stmt
*
stmt
);
bool
canChangePostfixToPrefix
(
const
Stmt
*
stmt
,
const
CXXOperatorCallExpr
*
op
);
bool
shouldDoChange
(
const
Expr
*
op
);
};
...
...
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