Skip to content

New conditional C Scanner; Improvements for virtual C Pre-Processor - #3053

Merged
bdbaddog merged 22 commits into
SCons:masterfrom
platformio:c-conditional-scanner
Apr 27, 2020
Merged

bdbaddog merged 22 commits into
SCons:masterfrom
platformio:c-conditional-scanner

Conversation

@ivankravets

@ivankravets ivankravets commented Jan 26, 2018 •

Copy link
Copy Markdown
Contributor

Hi all!

I've finally found a time to make a PR with conditional C Scanner which we use 2 years in production for @platformio. This PR is continue of a work on https://bitbucket.org/scons/scons/pull-requests/380/improvements-for-scons-c-c-pre-processor/diff

I made it maximum compatible with existing API, I even didn't touch original tests. The only incoming data for tests were extended to show that new changes don't break previous behaviour and provide a new conditional C Scanner.

Documentation? This is very complex question for me. I didn't check this item in a list below because I don't know where these changes should be reflected in documentation.

A default classic C Scanner is hardcoded here. Should we make additional option for SCons command which will allow to switch between Scanners?

Nevertheless, new conditional Scanner is a good addition for developers who create an own high level wrapper around SCons. We do this in PlatformIO and use CConditionalScanner to build a library dependency tree before calling SCons builder.

I'm ready for any corrections and any comments are welcome.

Changes

  • New conditional C Scanner (SCons.Scanner.C.CConditionalScanner())
    which interprets C/C Preprocessor conditional syntax (#ifdef, #if, #else,
    #elif, #define, etc.)
  • Improvements for virtual C Pre-Processor:
    • Handle UNSIGNED LONG and LONG numeric constants in DEC (keep support for HEX)
    • Skip unrecognized directives, such as #if( defined ...)
    • Ignore #include DYNAMIC_INCLUDE directive that depends on a dynamic
      macro which is not located in a state TABLE.
    • Cleanup CPP expressions before evaluating (strip comments, carriage returns)

Contributor Checklist:

  • I have created a new test or updated the unit tests to cover the new/changed functionality.
  • I have updated master/src/CHANGES.txt directory (and read the README.txt in that directory)
  • I have updated the appropriate documentation

@bdbaddog

Copy link
Copy Markdown
Contributor

Thanks for migrating this pull request over to Github.

@coveralls

coveralls commented Jan 26, 2018 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+1.8%) to 81.821% when pulling bbab871 on platformio:c-conditional-scanner into d36a52a on SCons:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-1.6%) to 82.558% when pulling 395e09a on platformio:c-conditional-scanner into 275cf49 on SCons:master.

* master: (270 commits)
  Update and add show option to cache config
  attempt fixing tests failing due to binary mode
  use absolute python path in sheabangs
  remove unnecessary sheabangs from scripts generated by end-to-end tests
  Correct sheabangs in code snippets under test/
  Update CHANGES.txt
  Fix GH Issue SCons#3225 Flatten() doesn't handle dictionary views produces by py3 dict().{items(), values(), keys()}
  Add testcase to demonstrate specifying dict.values() on py3 fails. as it returns a dict_values object and not a list
  PEP8
  Update CHANGES.txt
  Fix logic which populates JAVAINCLUDES when javac is not found. Restore checks in some java tests to skip test if no javac and/or jar found
  Additional fixes for SWIG tests
  Add blurb on Java fixes to CHANGES.txt
  Updated docs post updating Java tool xml file
  Add JAVAINCLUDES information
  Reordered and revised some text. Also added note about adding new info in order of contributors last name.
  Allow to override build date with SOURCE_DATE_EPOCH
  Get a little smarter about finding the scons engine.
  fix Value node tests to expect a string and not bytes
  remove editor junk
  ...
…ional-scanner

* 'master' of https://github.com/SCons/scons: (71 commits)
  Add Textfile/Substfile to default.
  add logic to remove bad xsltproc from appveyor ci machines if it's present. This is just to fix bogus failures for docbook/basic/*/*_cmd.py
  Remove delete of file. It's causing appveyor to die even though the command shouldn't issue an error code if xsltproc.exe is not there
  Modify changes.txt since this is a py3.5+ only issue.
  Fix cpp scanner regex logic to treat ifndef. Previously it was not properly differentiating between if, ifdef, and ifndef
  refine get_env_bool() docstrings
  Explicitly remove xsltproc.exe from git install. Somehow it's still there after update git install
  s/get_bool_envvar/get_os_env_bool/
  Fix duplicated items in src/CHANGES.txt
  Change test for str(node1) is str(node2) to use ==.  Expecting that the strings would have the same id() is not reasonable.  Expecting their values are equal is.
  Resolve comments from @garyo to clarify new code.
  Clarify and simplify logic in Node.get_binfo()
  Fix typos
  Fix docstring on FileBuildInfo per comment from @dirkbaechle
  Add test with MD5-timestamp decider and Repository usage
  fix whitespace issues
  cleanup travis.xml file
  add note about saxon-xslt version 5.5 needing xsl and source file argument order swapped
  switch from izip to zip. no izip in py3
  Revisit caching of filename -> csig map and invalidate when reasonable
  ...
Comment thread src/CHANGES.txt

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you dropped this in the merge..

@bdbaddog

Copy link
Copy Markdown
Contributor

I'd suggest adding an env variable like CC_SCANNER which defaults to current scanner, but can be assigned to your new one. That way we're not changing default behavior.

For docs you'd just had to add info on that new variable and the possible values.
Maybe add a section to Users guide section 27, miscellaneous functionality.

Also add a wiki page?

Tests would reset the CC_SCANNER to this more complete one and then run same tests as the current c scanner.
That should be sufficient to be able to merge this.

@bdbaddog

Copy link
Copy Markdown
Contributor

I'd love to merge this in as an optional scanner.
If I'm reading it correctly you've modified the existing scanner.
Given the depth of the change we'd minimally have to go through a deprecation cycle and do some evaluation of the performance vs existing scanner.

If you can change this to be a second optional C/C++ scanner. Then I can merge it.

@ivankravets

Copy link
Copy Markdown
Contributor Author

Thanks!

  1. I've just pushed new changes to our @platformio branch, now you will see only real modified files
  2. Yes, you are right, we created a separate scanner named CConditionalScanner
  3. "Given the depth ". Do you mean C-Preprocessor? The default value for depth is -1 which is the same which we have now in the master branch of the preprocessor. So, existing projects should not be affected.

@bdbaddog

Copy link
Copy Markdown
Contributor

@ivankravets - no, by depth I mean how that changing the C/C++ scanner is a nontrivial change.

So if I merge this it will or will not modify the existing C/C++ scanner?
#2 looks like your answering the inverse of the question I asked.

@ivankravets

Copy link
Copy Markdown
Contributor Author

Comment thread src/engine/SCons/Scanner/C.py Outdated
keyword, quote, fname = t
result = SCons.Node.FS.find_file(fname, self.searchpath[quote])
if not result:
paths = tuple(self._known_paths) + self.searchpath[quote]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you do have modifications to current C scanner here right?

@ivankravets

Copy link
Copy Markdown
Contributor Author

I've just isolated CConditionalScanner from an original scanner. Indeed, there is a bug in SConsCPPScanner.find_include_file which does not resolve correctly includes in " quotes which should be found based on the self.current_file directory.

I don't know if it is good if SConsCPPScanner will return the correct found items. Do you mean that it can break existing projects?

@bdbaddog

Copy link
Copy Markdown
Contributor

I'd rather you break that out into a separate pull request.
Is there an existing github issue for that?
Please separate that out to another PR, then it'll be easier to merge this and then take a look at that issue separately.?

@ivankravets

Copy link
Copy Markdown
Contributor Author

I already separated, see

@bdbaddog

Copy link
Copy Markdown
Contributor

You have both changes in this PR right? I'm asking if you can split them into a separate PR for the new scanner and one for the fix to the existing.

@ivankravets

Copy link
Copy Markdown
Contributor Author

No, I reverted back all my changes to the original scanner and duplicated it with modifications for the conditional preprocessor.

@ivankravets

Copy link
Copy Markdown
Contributor Author

I added a comment block

Screen Shot 2020-02-13 at 18 24 44

Comment thread src/engine/SCons/cpp.py
[r'//.*', ''],
[r'(0x[0-9A-Fa-f]*)[UL]+', '\\1'],
[r'defined\s+(\w+)', '"\\1" in __dict__'],
[r'defined\s*\((\w+)\)', '"\\1" in __dict__'],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is changed.

Comment thread src/engine/SCons/cpp.py
The main workhorse class for handling C pre-processing.
"""
def __init__(self, current=os.curdir, cpppath=(), dict={}, all=0):
def __init__(self, current=os.curdir, cpppath=(), dict={}, all=0, depth=-1):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is changed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is changed.

Comment thread src/engine/SCons/cpp.py
# Max depth of nested includes:
# -1 = unlimited
# 0 - disabled nesting
# >0 - number of allowed nested includes

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is changed.

@bdbaddog

Copy link
Copy Markdown
Contributor

Can you hop on discord #devel channel?

mwichmann added a commit to mwichmann/scons that referenced this pull request Apr 21, 2020
Extracted from PR SCons#3053 (Ivan Kravets) to make an isolated submission:
* Handle UNSIGNED LONG and LONG numeric constants in DEC (keep support for HEX)
* Skip unrecognized directives, such as `#if( defined ...)`
* Ignore `#include DYNAMIC_INCLUDE` directive that depends on a dynamic
macro which is not located in a state TABLE.
* Cleanup CPP expressions before evaluating (strip comments, carriage returns)

Signed-off-by: Mats Wichmann <[email protected]>
@mwichmann mwichmann mentioned this pull request Apr 21, 2020
3 tasks done
@bdbaddog

Copy link
Copy Markdown
Contributor

Sorry for my confusion on this PR.
It is as you said.
Also did some perf testing and no diff.
Merging.

@bdbaddog
bdbaddog merged commit de9ae74 into SCons:master Apr 27, 2020
@ivankravets

Copy link
Copy Markdown
Contributor Author

@bdbaddog Thanks for the merge!!! I'm so proud of this contribution!

Can you hop on discord #devel channel?

Sorry, it looks like I missed this comment :((((

@mwichmann

Copy link
Copy Markdown
Collaborator

Just a little curious... test/CPPDEFINES/scan.py emits a line

Scanner evaluation of CPP lines not yet supported; skipping test.

Do these changes affect that decision in any way?

@ivankravets

Copy link
Copy Markdown
Contributor Author

This commit should not affest test/CPPDEFINES/scan.py because it does not use conditional scanner. Maybe I don't know something :( How to debug this case?

@mwichmann

Copy link
Copy Markdown
Collaborator

No, I didn't mean you broke it; it's been this way (intentionally skipped) for a while. I wondered if enough scanning is available now that it could maybe be enabled for this scanner?

@ivankravets

Copy link
Copy Markdown
Contributor Author

Yes, it makes sense. There are some examples in src/engine/SCons/cppTests.py

We have been using it for ~4 years in production. Maybe our use case will help someone https://github.com/platformio/platformio-core/blob/develop/platformio/builder/tools/piolib.py#L97

In @platformio we thing called "Library Dependency Finder. So, thanks to the SCons and this Conditional Scanner, we actually automatically resolve all dependencies based on #include directives and build static libraries on-the-fly. Developers just install libraries from our registry and later just use #include <include/MyLib.h>, we do the rest of the work in auto mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants