Bug 1215238 - Mention the included filepath in pre-processed js sources with #includes. r=glandium

--HG--
extra : commitid : FJpPaYeSyUI
This commit is contained in:
Chris Manchester 2015-11-06 15:58:30 -08:00
Родитель cf2a0eeaa4
Коммит cf3bc2e02d
2 изменённых файлов: 68 добавлений и 11 удалений

Просмотреть файл

@ -288,7 +288,6 @@ class Preprocessor:
# 2: #else found
self.ifStates = []
self.checkLineNumbers = False
self.writtenLines = 0
self.filters = []
self.cmds = {}
for cmd, level in {'define': 0,
@ -403,6 +402,11 @@ class Preprocessor:
aLine = f[1](aLine)
return aLine
def noteLineInfo(self):
# Record the current line and file. Called once before transitioning
# into or out of an included file and after writing each line.
self.line_info = self.context['FILE'], self.context['LINE']
def write(self, aLine):
"""
Internal method for handling output.
@ -410,13 +414,16 @@ class Preprocessor:
if not self.out:
return
next_line, next_file = self.context['LINE'], self.context['FILE']
if self.checkLineNumbers:
self.writtenLines += 1
ln = self.context['LINE']
if self.writtenLines != ln:
self.out.write('//@line {line} "{file}"\n'.format(line=ln,
file=self.context['FILE']))
self.writtenLines = ln
expected_file, expected_line = self.line_info
expected_line += 1
if (expected_line != next_line or
expected_file and expected_file != next_file):
self.out.write('//@line {line} "{file}"\n'.format(line=next_line,
file=next_file))
self.noteLineInfo()
filteredLine = self.applyFilters(aLine)
if filteredLine != aLine:
self.actionLevel = 2
@ -541,7 +548,6 @@ class Preprocessor:
self.actionLevel = 2
elif self.disableLevel == 0 and not self.comment.match(aLine):
self.write(aLine)
pass
# Instruction handlers
# These are named do_'instruction name' and take one argument
@ -730,7 +736,6 @@ class Preprocessor:
Files should be opened, and will be closed after processing.
"""
isName = type(args) == str or type(args) == unicode
oldWrittenLines = self.writtenLines
oldCheckLineNumbers = self.checkLineNumbers
self.checkLineNumbers = False
if isName:
@ -749,6 +754,8 @@ class Preprocessor:
oldFile = self.context['FILE']
oldLine = self.context['LINE']
oldDir = self.context['DIRECTORY']
self.noteLineInfo()
if args.isatty():
# we're stdin, use '-' and '' for file and dir
self.context['FILE'] = '-'
@ -759,15 +766,15 @@ class Preprocessor:
self.context['FILE'] = abspath
self.context['DIRECTORY'] = os.path.dirname(abspath)
self.context['LINE'] = 0
self.writtenLines = 0
for l in args:
self.context['LINE'] += 1
self.handleLine(l)
if isName:
args.close()
self.context['FILE'] = oldFile
self.checkLineNumbers = oldCheckLineNumbers
self.writtenLines = oldWrittenLines
self.context['LINE'] = oldLine
self.context['DIRECTORY'] = oldDir
def do_includesubst(self, args):

Просмотреть файл

@ -556,6 +556,56 @@ class TestPreprocessor(unittest.TestCase):
self.pp.do_include('f')
self.assertEqual(self.pp.out.getvalue(), 'foobarbaz\nbarfoobaz\n')
def test_include_line(self):
files = {
'test.js': '\n'.join([
'#define foo foobarbaz',
'#include @inc@',
'@bar@',
'',
]),
'bar.js': '\n'.join([
'#define bar barfoobaz',
'@foo@',
'',
]),
'foo.js': '\n'.join([
'bazfoobar',
'#include bar.js',
'bazbarfoo',
'',
]),
'baz.js': 'baz\n',
'f.js': '\n'.join([
'#include foo.js',
'#filter substitution',
'#define inc bar.js',
'#include test.js',
'#include baz.js',
'fin',
'',
]),
}
with MockedOpen(files):
self.pp.do_include('f.js')
self.assertEqual(self.pp.out.getvalue(),
('//@line 1 "CWD/foo.js"\n'
'bazfoobar\n'
'//@line 2 "CWD/bar.js"\n'
'@foo@\n'
'//@line 3 "CWD/foo.js"\n'
'bazbarfoo\n'
'//@line 2 "CWD/bar.js"\n'
'foobarbaz\n'
'//@line 3 "CWD/test.js"\n'
'barfoobaz\n'
'//@line 1 "CWD/baz.js"\n'
'baz\n'
'//@line 6 "CWD/f.js"\n'
'fin\n').replace('CWD/',
os.getcwd() + os.path.sep))
def test_include_missing_file(self):
with MockedOpen({'f': '#include foo\n'}):
with self.assertRaises(Preprocessor.Error) as e: