File: Synopsis/Parsers/Cpp/__init__.py
 1#
 2# Copyright (C) 2003 Stefan Seefeld
 3# All rights reserved.
 4# Licensed to the public under the terms of the GNU LGPL (>= 2),
 5# see the file COPYING for details.
 6#
 7
 8"""Preprocessor for C, C++, IDL"""
 9
10from Synopsis.Processor import *
11from Emulator import get_compiler_info
12from ParserImpl import parse
13import os.path
14
15class Parser(Processor):
16
17    emulate_compiler = Parameter('', 'a compiler to emulate')
18    compiler_flags = Parameter([], 'list of flags for the emulated compiler')
19    flags = Parameter([], 'list of preprocessor flags such as -I or -D')
20    primary_file_only = Parameter(True, 'should only primary file be processed')
21    cpp_output = Parameter(None, 'filename for preprocessed file')
22    base_path = Parameter(None, 'path prefix to strip off of the filenames')
23    language = Parameter('C++', 'source code programming language of the given input file')
24
25    def probe(self, **kwds):
26
27        self.set_parameters(kwds)
28        if type(self.compiler_flags) != list:
29            raise InvalidArgument('compiler_flags=%s (expected list)'%repr(self.compiler_flags))
30        return get_compiler_info(self.language,
31                                 self.emulate_compiler,
32                                 self.compiler_flags)
33
34    def process(self, ir, **kwds):
35
36        self.set_parameters(kwds)
37        if not self.input: raise MissingArgument('input')
38        self.ir = ir
39
40        system_flags = []
41        # Accept either a string or a list.
42        flags = type(self.flags) is str and self.flags.split() or self.flags
43        base_path = self.base_path and os.path.abspath(self.base_path) + os.sep or ''
44        info = get_compiler_info(self.language,
45                                 self.emulate_compiler,
46                                 self.compiler_flags)
47        system_flags += ['-I%s'%x for x in info.include_paths]
48        system_flags += ['-D%s'%k + (v and '=%s'%v or '')
49                         for (k,v) in info.macros]
50        for file in self.input:
51            self.ir = parse(self.ir,
52                            os.path.abspath(file),
53                            base_path,
54                            self.cpp_output,
55                            self.language, system_flags, flags,
56                            self.primary_file_only,
57                            self.verbose, self.debug, self.profile)
58        return self.output_and_return_ir()
59
60