Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Q
qt-creator
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Tobias Hunger
qt-creator
Commits
a298759d
Commit
a298759d
authored
15 years ago
by
Christian Kamm
Browse files
Options
Downloads
Patches
Plain Diff
Quickfix: Add one that replaces + with % for strings.
Reviewed-by:
Roberto Raggi
<
roberto.raggi@nokia.com
>
parent
bcfb9d23
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/plugins/cppeditor/cppquickfix.cpp
+167
-0
167 additions, 0 deletions
src/plugins/cppeditor/cppquickfix.cpp
with
167 additions
and
0 deletions
src/plugins/cppeditor/cppquickfix.cpp
+
167
−
0
View file @
a298759d
...
...
@@ -39,6 +39,12 @@
#include
<ASTPatternBuilder.h>
#include
<ASTMatcher.h>
#include
<Token.h>
#include
<Type.h>
#include
<CoreTypes.h>
#include
<Symbol.h>
#include
<Symbols.h>
#include
<Name.h>
#include
<Literals.h>
#include
<cpptools/cppmodelmanagerinterface.h>
#include
<QtDebug>
...
...
@@ -899,6 +905,165 @@ private:
PostfixExpressionAST
*
qlatin1Call
;
};
/*
Replace
a + b
With
a % b
If a and b are of string type.
*/
class
UseFastStringConcatenation
:
public
QuickFixOperation
{
public:
UseFastStringConcatenation
()
{}
virtual
QString
description
()
const
{
return
QLatin1String
(
"Use fast string concatenation with %"
);
// ### tr?
}
virtual
int
match
(
const
QList
<
AST
*>
&
path
)
{
if
(
path
.
isEmpty
())
return
-
1
;
// need to search 'up' too
int
index
=
path
.
size
()
-
1
;
if
(
BinaryExpressionAST
*
binary
=
asPlusNode
(
path
[
index
]))
{
while
(
0
!=
(
binary
=
asPlusNode
(
binary
->
left_expression
)))
_binaryExpressions
.
prepend
(
binary
);
}
// search 'down'
for
(
index
=
path
.
size
()
-
1
;
index
!=
-
1
;
--
index
)
{
AST
*
node
=
path
.
at
(
index
);
if
(
BinaryExpressionAST
*
binary
=
asPlusNode
(
node
))
{
_binaryExpressions
.
append
(
binary
);
}
else
if
(
!
_binaryExpressions
.
isEmpty
())
{
break
;
}
}
if
(
_binaryExpressions
.
isEmpty
())
return
-
1
;
// verify types of arguments
BinaryExpressionAST
*
prevBinary
=
0
;
foreach
(
BinaryExpressionAST
*
binary
,
_binaryExpressions
)
{
if
(
binary
->
left_expression
!=
prevBinary
)
{
if
(
!
hasCorrectType
(
binary
->
left_expression
))
return
-
1
;
}
if
(
binary
->
right_expression
!=
prevBinary
)
{
if
(
!
hasCorrectType
(
binary
->
right_expression
))
return
-
1
;
}
prevBinary
=
binary
;
}
return
index
+
_binaryExpressions
.
size
();
}
virtual
void
createChangeSet
()
{
// replace + -> %
foreach
(
BinaryExpressionAST
*
binary
,
_binaryExpressions
)
replace
(
binary
->
binary_op_token
,
"%"
);
// wrap literals in QLatin1Literal
foreach
(
StringLiteralAST
*
literal
,
_stringLiterals
)
{
insert
(
startOf
(
literal
),
"QLatin1Literal("
);
insert
(
endOf
(
literal
),
")"
);
}
// replace QLatin1String/QString/QByteArray(literal) -> QLatin1Literal(literal)
foreach
(
PostfixExpressionAST
*
postfix
,
_incorrectlyWrappedLiterals
)
{
replace
(
postfix
->
base_expression
,
"QLatin1Literal"
);
}
}
BinaryExpressionAST
*
asPlusNode
(
AST
*
ast
)
{
BinaryExpressionAST
*
binary
=
ast
->
asBinaryExpression
();
if
(
binary
&&
tokenAt
(
binary
->
binary_op_token
).
kind
()
==
T_PLUS
)
return
binary
;
return
0
;
}
bool
hasCorrectType
(
ExpressionAST
*
ast
)
{
if
(
StringLiteralAST
*
literal
=
ast
->
asStringLiteral
())
{
_stringLiterals
+=
literal
;
return
true
;
}
if
(
PostfixExpressionAST
*
postfix
=
ast
->
asPostfixExpression
())
{
if
(
postfix
->
base_expression
&&
postfix
->
postfix_expression_list
&&
postfix
->
postfix_expression_list
->
value
&&
!
postfix
->
postfix_expression_list
->
next
)
{
NameAST
*
name
=
postfix
->
base_expression
->
asName
();
CallAST
*
call
=
postfix
->
postfix_expression_list
->
value
->
asCall
();
if
(
name
&&
call
)
{
QByteArray
nameStr
(
name
->
name
->
identifier
()
->
chars
());
if
((
nameStr
==
"QLatin1String"
||
nameStr
==
"QString"
||
nameStr
==
"QByteArray"
)
&&
call
->
expression_list
&&
call
->
expression_list
->
value
&&
call
->
expression_list
->
value
->
asStringLiteral
()
&&
!
call
->
expression_list
->
next
)
{
_incorrectlyWrappedLiterals
+=
postfix
;
return
true
;
}
}
}
}
const
QList
<
LookupItem
>
&
lookup
=
typeOf
(
ast
);
if
(
lookup
.
isEmpty
())
return
false
;
return
isQtStringType
(
lookup
[
0
].
type
());
}
bool
isBuiltinStringType
(
FullySpecifiedType
type
)
{
// char*
if
(
PointerType
*
ptrTy
=
type
->
asPointerType
())
if
(
IntegerType
*
intTy
=
ptrTy
->
elementType
()
->
asIntegerType
())
if
(
intTy
->
kind
()
==
IntegerType
::
Char
)
return
true
;
return
false
;
}
bool
isQtStringType
(
FullySpecifiedType
type
)
{
if
(
NamedType
*
nameTy
=
type
->
asNamedType
())
{
if
(
!
nameTy
->
name
()
||
!
nameTy
->
name
()
->
identifier
())
return
false
;
QByteArray
name
(
nameTy
->
name
()
->
identifier
()
->
chars
());
if
(
name
==
"QString"
||
name
==
"QByteArray"
||
name
==
"QLatin1String"
||
name
==
"QLatin1Literal"
||
name
==
"QStringRef"
||
name
==
"QChar"
)
return
true
;
}
return
false
;
}
private
:
QList
<
BinaryExpressionAST
*>
_binaryExpressions
;
QList
<
StringLiteralAST
*>
_stringLiterals
;
QList
<
PostfixExpressionAST
*>
_incorrectlyWrappedLiterals
;
};
}
// end of anonymous namespace
...
...
@@ -1211,6 +1376,7 @@ int CPPQuickFixCollector::startCompletion(TextEditor::ITextEditable *editable)
QSharedPointer
<
FlipBinaryOp
>
flipBinaryOp
(
new
FlipBinaryOp
());
QSharedPointer
<
WrapStringLiteral
>
wrapStringLiteral
(
new
WrapStringLiteral
());
QSharedPointer
<
CStringToNSString
>
wrapCString
(
new
CStringToNSString
());
QSharedPointer
<
UseFastStringConcatenation
>
useFastStringConcat
(
new
UseFastStringConcatenation
());
QList
<
QuickFixOperationPtr
>
candidates
;
candidates
.
append
(
rewriteLogicalAndOp
);
...
...
@@ -1223,6 +1389,7 @@ int CPPQuickFixCollector::startCompletion(TextEditor::ITextEditable *editable)
candidates
.
append
(
flipBinaryOp
);
candidates
.
append
(
wrapStringLiteral
);
candidates
.
append
(
wrapCString
);
candidates
.
append
(
useFastStringConcat
);
QMap
<
int
,
QList
<
QuickFixOperationPtr
>
>
matchedOps
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment