-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-133228: c-analyzer clang preprocessor #133229
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
Open
dg-pb
wants to merge
5
commits into
python:main
Choose a base branch
from
dg-pb:c_analyzer_clang_experimental_preprocessor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+125
−3
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import os.path | ||
import re, sys | ||
|
||
from . import common as _common | ||
from . import gcc as _gcc | ||
|
||
_normpath = _gcc._normpath | ||
|
||
TOOL = 'clang' | ||
|
||
META_FILES = { | ||
'<built-in>', | ||
'<command line>', | ||
} | ||
|
||
|
||
def preprocess(filename, | ||
incldirs=None, | ||
includes=None, | ||
macros=None, | ||
samefiles=None, | ||
cwd=None, | ||
): | ||
if not cwd or not os.path.isabs(cwd): | ||
cwd = os.path.abspath(cwd or '.') | ||
filename = _normpath(filename, cwd) | ||
|
||
postargs = _gcc.POST_ARGS | ||
basename = os.path.basename(filename) | ||
dirname = os.path.basename(os.path.dirname(filename)) | ||
if (basename not in _gcc.FILES_WITHOUT_INTERNAL_CAPI | ||
and dirname not in _gcc.DIRS_WITHOUT_INTERNAL_CAPI): | ||
postargs += ('-DPy_BUILD_CORE=1',) | ||
|
||
text = _common.preprocess( | ||
TOOL, | ||
filename, | ||
incldirs=incldirs, | ||
includes=includes, | ||
macros=macros, | ||
#preargs=PRE_ARGS, | ||
postargs=postargs, | ||
executable=['clang'], | ||
compiler='unix', | ||
cwd=cwd, | ||
) | ||
return _iter_lines(text, filename, samefiles, cwd) | ||
|
||
|
||
EXIT_MARKERS = {'# 2 "<built-in>" 2', '# 3 "<built-in>" 2', '# 4 "<built-in>" 2'} | ||
|
||
|
||
def _iter_lines(text, reqfile, samefiles, cwd, raw=False): | ||
lines = iter(text.splitlines()) | ||
|
||
# The first line is special. | ||
# The subsequent lines are consistent. | ||
firstlines = [ | ||
f'# 1 "{reqfile}"', | ||
'# 1 "<built-in>" 1', | ||
'# 1 "<built-in>" 3', | ||
'# 370 "<built-in>" 3', | ||
'# 1 "<command line>" 1', | ||
'# 1 "<built-in>" 2', | ||
] | ||
for expected in firstlines: | ||
line = next(lines) | ||
if line != expected: | ||
raise NotImplementedError((line, expected)) | ||
|
||
# Do all the CLI-provided includes. | ||
filter_reqfile = (lambda f: _gcc._filter_reqfile(f, reqfile, samefiles)) | ||
make_info = (lambda lno: _common.FileInfo(reqfile, lno)) | ||
last = None | ||
for line in lines: | ||
assert last != reqfile, (last,) | ||
# NOTE:condition is clang specific | ||
if not line: | ||
continue | ||
lno, included, flags = _gcc._parse_marker_line(line, reqfile) | ||
if not included: | ||
raise NotImplementedError((line,)) | ||
if included == reqfile: | ||
# This will be the last one. | ||
assert 2 in flags, (line, flags) | ||
else: | ||
# NOTE:first condition is specific to clang | ||
if _normpath(included, cwd) == reqfile: | ||
assert 1 in flags or 2 in flags, (line, flags, included, reqfile) | ||
else: | ||
assert 1 in flags, (line, flags, included, reqfile) | ||
yield from _gcc._iter_top_include_lines( | ||
lines, | ||
_normpath(included, cwd), | ||
cwd, | ||
filter_reqfile, | ||
make_info, | ||
raw, | ||
EXIT_MARKERS | ||
) | ||
last = included | ||
# The last one is always the requested file. | ||
# NOTE:_normpath is clang specific | ||
assert _normpath(included, cwd) == reqfile, (line,) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Would it make sense to move the parts used from
gcc
in this new clang module tocommon
?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.
It would, but I would leave this for when this is stable. And would need to look into it a bit more to get a sense what can be common and what is best to be left separate.
For now I would not rather mess with gcc.
And will add a warning that this is experimental with a list of skipped files.
Any chance you are running osx and could check if it works for you?
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.
Windows, sorry.