-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Special-case ## pasting to string/character constants (issue #168) #255
Conversation
This enables use of macros to add literals/operator "".
simplecpp.cpp
Outdated
|
||
static bool isCharConstant(const std::string &s) | ||
{ | ||
return s.size() == 3 && (s[0]=='\'') && (s[2]=='\''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imho this should also be classified as a char constant: '\t'
.
a multi char constant is not handled: 'ab'
. is that intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, good point (and now the comment about ''' in the original thread makes way more sense).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
multichar constants don't seem to work with operator "" though, so that might be accidentally intentional. Will investigate further.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the pragmatic approach seems to be to relax the restriction to s.size() > 2
. That covers ‘a’
, ‘1234’
, ‘\t’
, ‘\000’
etc. but doesn't check the validity, which seem in line with the regular output. Or even > 1 analog to strings since there’s the weird case of ’’
.
Multi-character plus user-defined operator "" don't seem to actually compile but clang/gcc will happily preprocess it with -E
(godbolt).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know about ''
, yes that is weird :-)
So yes I think the pragmatic approach sounds good.
Additional/renamed tests.
So, some additional cases to consider down the literals rabbit hole...
results in
Interestingly the string with
There already exists a |
yay!! I think some users will be very happy! |
I assumed this could be investigated in separate PRs. If you want to continue working on that feel free to do it. Otherwise I hope we can open some tickets. The problems you see.. is it simplecpp bugs or cppcheck bugs? |
Great, thanks for the review! |
Building on the approach from PR #169 this special cases the
##
pasting inexpandHashHash
for string and character literals to allow for (user-defined) literals (operator “”
).if
block; it’s not necessarily elegant but trying to maintain a single tokensB/strAB path through the other branches seems harder to follow (*).hashhash
test cases. Suggestions for other/better boundary cases welcome.hashhash18
,hashhash19
) the result of ## isn’t actually useful (and does eventually produce a syntax error incppcheck
). Detecting that seems like a different rabbit hole.(*) although now it’s not a trivial merge into cppcheck tree. Can’t win…
(**) As an aside there already exists a difference between the handling of
operator “”
applied to strings/chars or numbers even without macros. But at least with/without macros its now consistent, so this is perhaps a follow-up item.