[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[cdt-dev] Using CDT to parse C++ macros?
|
Hi all,
Searching into mailing lists, I found this message
http://dev.eclipse.org/mhonarc/lists/cdt-dev/msg10285.html which explains how
to use CDT parser as a stand-alone module:
import java.util.HashMap;
import java.util.Map;
import org.eclipse.cdt.core.dom.ICodeReaderFactory;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.GPPLanguage;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.DefaultLogService;
import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.internal.core.parser.scanner2.FileCodeReaderFactory;
public class ParserExample {
public static void main(String[] args) throws Exception {
IParserLogService log = new DefaultLogService();
String code = "class Class { public: int x,y; Class();
~Class(); private: Class f(); }; int function(double parameter) { return
parameter; };";
CodeReader reader = new CodeReader(code.toCharArray());
Map definedSymbols = new HashMap();
String[] includePaths = new String[0];
IScannerInfo info = new ScannerInfo(definedSymbols,
includePaths);
ICodeReaderFactory readerFactory =
FileCodeReaderFactory.getInstance();
IASTTranslationUnit translationUnit =
GPPLanguage.getDefault().getASTTranslationUnit(reader, info, readerFactory,
null, log);
ASTVisitor visitor = new ASTVisitor() {
public int visit(IASTName name) {
System.out.println(name.getRawSignature());
return ASTVisitor.PROCESS_CONTINUE;
}
};
visitor.shouldVisitNames = true;
translationUnit.accept(visitor);
}
}
I would like to to parse C++ macros such as: #define N 7, #define ADD_FIVE(a)
(a) + 5, #define getmax(a,b) a>b?a:b ... Is this possible using CDT parser? or
I have to preprocess source files before doing it?
Browsing source code I found a visit method for macro statements in
ASTVitor.class in org.eclipse.cdt.core.dom.ast package - public int
visit(IASTPreprocessorStatement PreprocessorStatement) -, so, I think, it
could be possible to collect macro information from AST using this method but
I don't know how to configure the parser to do it. Could you help me?
Regards