-
Notifications
You must be signed in to change notification settings - Fork 365
C++: support alternative operators #1572
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
C++: support alternative operators #1572
Conversation
ivangalkin
left a comment
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.
Please split the change into the
- fixing the binary repository (already done #1571 )
- fixing the parsing (started with #1570, but tests and the grammar adjustment is missing)
After that I will merge the #1571 (normally @guwirth does it, but this is an important and blocking PR) and we will rebase remaining PRs on top of that. Thank you in advance.
e08531f to
9a14ea1
Compare
9a14ea1 to
452ce54
Compare
|
From: #1570 (comment)
This branch now contains both fixes. But, from the comment above:
If you're able to give me a hint where to change the tests and the grammar adjustments that would be great. Thanks |
ivangalkin
left a comment
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.
Reviewed 2 of 3 files at r1.
Reviewable status: 2 of 3 files reviewed, 1 unresolved discussion (waiting on @moriarty, @jmecosta, @Bertk, and @guwirth)
cxx-squid/src/main/java/org/sonar/cxx/parser/CxxGrammarImpl.java, line 579 at r1 (raw file):
AND_EQ
copy-n-paste error; must be OR_EQ
Try to fix SonarOpenCommunity#1569. This adds the alternative operators keywords after they occured in the CxxGrammar. See: https://en.cppreference.com/w/cpp/language/operator_alternative Includes fixes from review
452ce54 to
e21003c
Compare
|
@ivangalkin I've fixed the copy-and-paste error. I amended the commit and did a force push out of habit... I meant to check the CONTRIBUTING.md first... I don't know how that works with this reviawable app, but here is the diff. index 70c57d22..25c23559 100644
--- a/cxx-squid/src/main/java/org/sonar/cxx/parser/CxxGrammarImpl.java
+++ b/cxx-squid/src/main/java/org/sonar/cxx/parser/CxxGrammarImpl.java
@@ -576,7 +576,7 @@ public enum CxxGrammarImpl implements GrammarRuleKey {
b.rule(foldOperator).is(
b.firstOf( // C++
"+", "-", "*", "/", "%", "ˆ", CxxKeyword.XOR, "&", CxxKeyword.BITAND, "|", CxxKeyword.BITOR, "<<", ">>",
- "+=", "-=", "*=", "/=", "%=", "ˆ=", CxxKeyword.XOR_EQ, "&=", CxxKeyword.AND_EQ, "|=", CxxKeyword.AND_EQ, "<<=", ">>=", "=",
+ "+=", "-=", "*=", "/=", "%=", "ˆ=", CxxKeyword.XOR_EQ, "&=", CxxKeyword.AND_EQ, "|=", CxxKeyword.OR_EQ, "<<=", ">>=", "=",
"==", "!=", CxxKeyword.NOT_EQ, "<", ">", "<=", ">=", "&&", CxxKeyword.AND, "||", CxxKeyword.OR, ",", ".*", "->*"
)
);
|
moriarty
left a comment
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.
Reviewable status: 2 of 3 files reviewed, 1 unresolved discussion (waiting on @ivangalkin, @jmecosta, @Bertk, and @guwirth)
cxx-squid/src/main/java/org/sonar/cxx/parser/CxxGrammarImpl.java, line 579 at r1 (raw file):
Previously, ivangalkin wrote…
AND_EQcopy-n-paste error; must be OR_EQ
Done.
ivangalkin
left a comment
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.
Reviewed 1 of 3 files at r1.
Reviewable status: 2 of 3 files reviewed, all discussions resolved (waiting on @ivangalkin, @jmecosta, @Bertk, and @guwirth)
|
@moriarty thank you!
So at the first glance the solution looks valid to me. There are at least 2 cases where according to the clang parser the tokens of alternative operators are considered to be identifiers (see below), but probably (?) we can ignore them. /// C++11 [dcl.attr.grammar]p3:
/// If a keyword or an alternative token that satisfies the syntactic
/// requirements of an identifier is contained in an attribute-token,
/// it is considered an identifier. // In Objective-C++, alternative operator tokens can be used as keyword args
// in message expressions. Unconsume the token so that it can reinterpreted
// as an identifier in ParseObjCMessageExpressionBody. i.e., we support:
// [foo meth:0 and:0];
// [foo not_eq]; |
guwirth
left a comment
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.
@moriarty thx for providing this. Can’t remember why this was not supported in the past? Only thing I’m wondering: why is it not enough to do that in the preprocessor? Using header file iso646.h is also working?
|
Hi, I'm not sure about including I tried switching them from |
|
Hi @moriarty the idea is to use |
|
I tried with
--- a/sonar-project.properties
+++ b/sonar-project.properties
@@ -2,6 +2,7 @@ sonar.projectKey=some-project
sonar.sources=src
sonar.tests=test
sonar.language=c++
+sonar.cxx.forceIncludes=iso646.h
sonar.cxx.defines=__GNUC__ 7 \n\ __GNUC_MINOR__ 3 \n\ BOOST_VERSION 106501 \n\ TEST_MODE 0 \n\ SILENT 0
And still get the errors as reported in #1569 But if I change that line to |
|
I just saw your link to #778... If that solution worked for others, then maybe the include didn't work due to Update: now I've added |
|
@moriarty thx for testing. Let’s wait for other opinions. I always like to keep the grammar as small as possible. |
|
The solutions proposed did not work. I tried to include iso646.h first via the sonar-project.properties file, and then manually in a few files. Here is the contents of the file: /* Copyright (C) 1997-2017 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */
/*
* ISO C Standard: 7.9 Alternative spellings <iso646.h>
*/
#ifndef _ISO646_H
#define _ISO646_H
#ifndef __cplusplus
#define and &&
#define and_eq &=
#define bitand &
#define bitor |
#define compl ~
#define not !
#define not_eq !=
#define or ||
#define or_eq |=
#define xor ^
#define xor_eq ^=
#endif
#endif
and the file came from: A workaround, which changed the warnings shown to the terminal, was to add them all as defines to the sonar-project.properties file.. sonar.sources=src
sonar.tests=test
sonar.language=c++
-sonar.cxx.defines=__GNUC__ 7 \n\ __GNUC_MINOR__ 3 \n\ BOOST_VERSION 106501 \n\ TEST_MODE 0 \n\ SILENT 0
+sonar.cxx.defines=and && \n\ and_eq &= \n\ bitand & \n\ bitor | \n\ compl ~ \n\ not ! \n\ not_eq != \n\ or || \n\ or_eq |= \n\ xor ^ \n\ xor_eq ^= \n\ __GNUC__ 7 \n\ __GNUC_MINOR__ 3 \n\ BOOST_VERSION 106501 \n\ TEST_MODE 0 \n\ SILENT 0
But now I get a lot of new warnings, instead of the old one. I don't mind a few sonar.cxx.defines. As a developer I know why the GNU 7 and BOOST_VERSION 106501 defines are there, they're to support two APIs (but it's unfortunately only possible to cover one using this sonar-cxx plugin, so they're scoped to just one line around the call which changed it's API. But it feels wrong to need to add all of the alternative representations as sonar.cxx.defines to make the parser not skip valid c++. |
|
@moriarty & @ivangalkin see two possibilities:
What do you think? |
|
@guwirth I would personally vote for the grammar extension. In my judgement one cannot relate on our preprocessor implementation. The patch doesn't bloat our grammar significantly and solves the problem with alternative operators once and forever (hopefully). |
Fix for #1569
This is branched off of #1571 because I wanted the tests to be run... I'm not really familiar with the project or syntax... Setting up the testing etc was a bit of work, so I aim relaying on Travis and the code reviewers.
I found in #1570 that the alternative operators already existed, in two locations:
sonar/cxx/api/CxxKeyword.java
sonar/cxx/api/CppPunctuator.java
I used the ones from CxxKeywords and I just added them after where the regular ones were found.
This change is