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
4d7b194d
Kaydet (Commit)
4d7b194d
authored
Nis 23, 2015
tarafından
Stephan Bergmann
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Improved loplugin:literaltoboolconversion looking into cond. exprs.
Change-Id: If54ab99fc82c7895da6bb88ebf18a11570f597ed
üst
22401181
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
63 additions
and
42 deletions
+63
-42
literaltoboolconversion.cxx
compilerplugins/clang/literaltoboolconversion.cxx
+63
-42
No files found.
compilerplugins/clang/literaltoboolconversion.cxx
Dosyayı görüntüle @
4d7b194d
...
...
@@ -29,6 +29,9 @@ public:
private
:
bool
isFromCIncludeFile
(
SourceLocation
spellingLocation
)
const
;
void
handleImplicitCastSubExpr
(
ImplicitCastExpr
const
*
castExpr
,
Expr
const
*
subExpr
);
};
bool
LiteralToBoolConversion
::
VisitImplicitCastExpr
(
...
...
@@ -40,25 +43,53 @@ bool LiteralToBoolConversion::VisitImplicitCastExpr(
if
(
!
expr
->
getType
()
->
isBooleanType
())
{
return
true
;
}
Expr
const
*
sub
=
expr
->
getSubExpr
()
->
IgnoreParenCasts
();
Expr
const
*
expr2
=
expr
;
handleImplicitCastSubExpr
(
expr
,
expr
->
getSubExpr
());
return
true
;
}
bool
LiteralToBoolConversion
::
isFromCIncludeFile
(
SourceLocation
spellingLocation
)
const
{
return
!
compat
::
isInMainFile
(
compiler
.
getSourceManager
(),
spellingLocation
)
&&
(
StringRef
(
compiler
.
getSourceManager
().
getPresumedLoc
(
spellingLocation
)
.
getFilename
())
.
endswith
(
".h"
));
}
void
LiteralToBoolConversion
::
handleImplicitCastSubExpr
(
ImplicitCastExpr
const
*
castExpr
,
Expr
const
*
subExpr
)
{
Expr
const
*
expr2
=
subExpr
;
// track sub-expr with potential parens, to e.g. rewrite all of expanded
//
// #define sal_False ((sal_Bool)0)
//
// including the parens
subExpr
=
expr2
->
IgnoreParenCasts
();
for
(;;)
{
BinaryOperator
const
*
op
=
dyn_cast
<
BinaryOperator
>
(
sub
);
BinaryOperator
const
*
op
=
dyn_cast
<
BinaryOperator
>
(
sub
Expr
);
if
(
op
==
nullptr
||
op
->
getOpcode
()
!=
BO_Comma
)
{
break
;
}
expr2
=
op
->
getRHS
()
->
IgnoreParenCasts
()
;
sub
=
expr2
;
expr2
=
op
->
getRHS
();
sub
Expr
=
expr2
->
IgnoreParenCasts
()
;
}
if
(
sub
->
getType
()
->
isBooleanType
())
{
return
true
;
if
(
subExpr
->
getType
()
->
isBooleanType
())
{
return
;
}
ConditionalOperator
const
*
op
=
dyn_cast
<
ConditionalOperator
>
(
subExpr
);
if
(
op
!=
nullptr
)
{
handleImplicitCastSubExpr
(
castExpr
,
op
->
getTrueExpr
());
handleImplicitCastSubExpr
(
castExpr
,
op
->
getFalseExpr
());
return
;
}
APSInt
res
;
if
(
!
sub
->
isValueDependent
()
&&
sub
->
isIntegerConstantExpr
(
res
,
compiler
.
getASTContext
())
if
(
!
sub
Expr
->
isValueDependent
()
&&
sub
Expr
->
isIntegerConstantExpr
(
res
,
compiler
.
getASTContext
())
&&
res
.
getLimitedValue
()
<=
1
)
{
SourceLocation
loc
{
sub
->
getLocStart
()
};
SourceLocation
loc
{
sub
Expr
->
getLocStart
()
};
while
(
compiler
.
getSourceManager
().
isMacroArgExpansion
(
loc
))
{
loc
=
compiler
.
getSourceManager
().
getImmediateMacroCallerLoc
(
loc
);
}
...
...
@@ -72,46 +103,46 @@ bool LiteralToBoolConversion::VisitImplicitCastExpr(
if
(
isFromCIncludeFile
(
compiler
.
getSourceManager
().
getSpellingLoc
(
loc
)))
{
return
true
;
return
;
}
}
}
if
(
isa
<
StringLiteral
>
(
sub
))
{
SourceLocation
loc
{
sub
->
getLocStart
()
};
if
(
isa
<
StringLiteral
>
(
sub
Expr
))
{
SourceLocation
loc
{
sub
Expr
->
getLocStart
()
};
if
(
compiler
.
getSourceManager
().
isMacroArgExpansion
(
loc
)
&&
(
Lexer
::
getImmediateMacroName
(
loc
,
compiler
.
getSourceManager
(),
compiler
.
getLangOpts
())
==
"assert"
))
{
return
true
;
return
;
}
}
if
(
isa
<
IntegerLiteral
>
(
sub
)
||
isa
<
CharacterLiteral
>
(
sub
)
||
isa
<
FloatingLiteral
>
(
sub
)
||
isa
<
ImaginaryLiteral
>
(
sub
)
||
isa
<
StringLiteral
>
(
sub
))
if
(
isa
<
IntegerLiteral
>
(
sub
Expr
)
||
isa
<
CharacterLiteral
>
(
subExpr
)
||
isa
<
FloatingLiteral
>
(
sub
Expr
)
||
isa
<
ImaginaryLiteral
>
(
subExpr
)
||
isa
<
StringLiteral
>
(
sub
Expr
))
{
bool
rewritten
=
false
;
if
(
rewriter
!=
nullptr
)
{
SourceLocation
loc
{
compiler
.
getSourceManager
().
getExpansionLoc
(
expr
->
getLocStart
())
};
if
(
compiler
.
getSourceManager
().
getExpansionLoc
(
expr
->
getLocEnd
())
expr
2
->
getLocStart
())
};
if
(
compiler
.
getSourceManager
().
getExpansionLoc
(
expr
2
->
getLocEnd
())
==
loc
)
{
char
const
*
s
=
compiler
.
getSourceManager
().
getCharacterData
(
loc
);
unsigned
n
=
Lexer
::
MeasureTokenLength
(
expr
->
getLocEnd
(),
compiler
.
getSourceManager
(),
expr
2
->
getLocEnd
(),
compiler
.
getSourceManager
(),
compiler
.
getLangOpts
());
std
::
string
tok
{
s
,
n
};
if
(
tok
==
"sal_False"
||
tok
==
"0"
)
{
rewritten
=
replaceText
(
compiler
.
getSourceManager
().
getExpansionLoc
(
expr
->
getLocStart
()),
expr
2
->
getLocStart
()),
n
,
"false"
);
}
else
if
(
tok
==
"sal_True"
||
tok
==
"1"
)
{
rewritten
=
replaceText
(
compiler
.
getSourceManager
().
getExpansionLoc
(
expr
->
getLocStart
()),
expr
2
->
getLocStart
()),
n
,
"true"
);
}
}
...
...
@@ -121,10 +152,10 @@ bool LiteralToBoolConversion::VisitImplicitCastExpr(
DiagnosticsEngine
::
Warning
,
"implicit conversion (%0) of literal of type %1 to %2"
,
expr2
->
getLocStart
())
<<
expr
->
getCastKindName
()
<<
expr
->
getSubExpr
()
->
getType
()
<<
e
xpr
->
getType
()
<<
expr2
->
getSourceRange
();
<<
castExpr
->
getCastKindName
()
<<
subExpr
->
getType
()
<<
castE
xpr
->
getType
()
<<
expr2
->
getSourceRange
();
}
}
else
if
(
sub
->
isNullPointerConstant
(
}
else
if
(
sub
Expr
->
isNullPointerConstant
(
compiler
.
getASTContext
(),
Expr
::
NPC_ValueDependentIsNull
)
>
Expr
::
NPCK_ZeroExpression
)
{
...
...
@@ -138,30 +169,20 @@ bool LiteralToBoolConversion::VisitImplicitCastExpr(
(
"implicit conversion (%0) of null pointer constant of type %1 to"
" %2"
),
expr2
->
getLocStart
())
<<
expr
->
getCastKindName
()
<<
expr
->
getSubExpr
()
->
getType
()
<<
e
xpr
->
getType
()
<<
expr2
->
getSourceRange
();
}
else
if
(
!
sub
->
isValueDependent
()
&&
sub
->
isIntegerConstantExpr
(
res
,
compiler
.
getASTContext
()))
<<
castExpr
->
getCastKindName
()
<<
subExpr
->
getType
()
<<
castE
xpr
->
getType
()
<<
expr2
->
getSourceRange
();
}
else
if
(
!
sub
Expr
->
isValueDependent
()
&&
sub
Expr
->
isIntegerConstantExpr
(
res
,
compiler
.
getASTContext
()))
{
report
(
DiagnosticsEngine
::
Warning
,
(
"implicit conversion (%0) of integer constant expression of type"
" %1 with value %2 to %3"
),
expr2
->
getLocStart
())
<<
expr
->
getCastKindName
()
<<
expr
->
getSubExpr
()
->
getType
()
<<
res
.
toString
(
10
)
<<
expr
->
getType
()
<<
expr2
->
getSourceRange
();
<<
castExpr
->
getCastKindName
()
<<
subExpr
->
getType
()
<<
res
.
toString
(
10
)
<<
castExpr
->
getType
()
<<
expr2
->
getSourceRange
();
}
return
true
;
}
bool
LiteralToBoolConversion
::
isFromCIncludeFile
(
SourceLocation
spellingLocation
)
const
{
return
!
compat
::
isInMainFile
(
compiler
.
getSourceManager
(),
spellingLocation
)
&&
(
StringRef
(
compiler
.
getSourceManager
().
getPresumedLoc
(
spellingLocation
)
.
getFilename
())
.
endswith
(
".h"
));
}
loplugin
::
Plugin
::
Registration
<
LiteralToBoolConversion
>
X
(
...
...
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