29package com.jogamp.gluegen.test.junit.generation;
31import com.jogamp.common.os.AndroidVersion;
32import com.jogamp.gluegen.ConstantDefinition;
33import com.jogamp.gluegen.JavaConfiguration;
34import com.jogamp.gluegen.cgram.CToken;
35import com.jogamp.gluegen.cgram.Define;
36import com.jogamp.gluegen.cgram.GNUCTokenTypes;
37import com.jogamp.gluegen.cgram.GnuCLexer;
38import com.jogamp.gluegen.cgram.GnuCParser;
39import com.jogamp.gluegen.cgram.HeaderParser;
40import com.jogamp.gluegen.cgram.TNode;
41import com.jogamp.gluegen.cgram.types.EnumType;
42import com.jogamp.gluegen.cgram.types.TypeDictionary;
43import com.jogamp.gluegen.jcpp.JCPP;
44import com.jogamp.gluegen.jcpp.LexerException;
45import com.jogamp.gluegen.jcpp.Macro;
46import com.jogamp.junit.util.SingletonJunitCase;
48import antlr.RecognitionException;
49import antlr.TokenStreamException;
50import antlr.TokenStreamRecognitionException;
51import junit.framework.Assert;
53import java.io.BufferedReader;
54import java.io.ByteArrayOutputStream;
55import java.io.DataInputStream;
57import java.io.FileInputStream;
58import java.io.FileNotFoundException;
59import java.io.FileOutputStream;
60import java.io.FileReader;
61import java.io.IOException;
62import java.util.Collections;
64import org.junit.BeforeClass;
66import static org.junit.Assert.*;
72import org.junit.FixMethodOrder;
73import org.junit.runners.MethodSorters;
75@FixMethodOrder(MethodSorters.NAME_ASCENDING)
78 static final String sourcePath = BuildEnvironment.gluegenRoot +
"/src/junit/com/jogamp/gluegen/test/junit/generation/";
79 static final boolean debug =
false;
82 public static void init() {
85 setTestSupported(
false);
91 if(
null != test10CCFileName ) {
96 final String cppResultPath = cpp(
"cpptest_1",
".h", debug);
97 cc(cppResultPath, debug);
98 }
catch (RecognitionException | IOException | LexerException e) {
109 if(
null != test10CCFileName ) {
110 cc(test10CCFileName, debug);
112 cc(sourcePath +
"cpptest_10.hpp", debug);
114 }
catch (RecognitionException | IOException | LexerException e) {
121 public String
cpp(
final String cSourceBasename,
final String cSourceSuffix,
final boolean debug)
throws FileNotFoundException, IOException, LexerException, RecognitionException {
122 final String cSourcePath = sourcePath + cSourceBasename + cSourceSuffix;
123 final FileReader cSourceReader =
new FileReader(cSourcePath);
125 final String cppResultPath = BuildEnvironment.testOutput +
"/" + cSourceBasename +
".hpp";
126 final File cppResultFile =
new File( cppResultPath );
127 if( cppResultFile.exists() ) {
128 cppResultFile.delete();
131 System.err.println(
"XXX JCPP: "+cSourcePath);
132 System.err.println(
"XXX cpp result-file "+cppResultFile);
133 try(
final FileOutputStream cppResultOStream =
new FileOutputStream(cppResultFile) ) {
134 final JCPP pp =
new JCPP(Collections.<String>singletonList(sourcePath), debug,
false,
true );
135 pp.addDefine(
"__GLUEGEN__",
"2");
136 pp.setOut(cppResultOStream);
137 pp.run(
new BufferedReader(cSourceReader), cSourceBasename);
138 cppResultOStream.flush();
139 cppResultOStream.close();
142 for (
final Macro cdef : pp.cpp.getMacros(
true)) {
143 System.err.println(
"XXX cpp Macr "+macroCount+
" <"+cdef+
">, isFunc "+
144 cdef.isFunctionLike()+
", isConstExpr "+
152 System.err.println(
"XXX cpp Defn "+defCount+
" <"+cdef+
">");
158 return cppResultPath;
161 public void cc(
final String cppResultPath,
final boolean debug)
throws FileNotFoundException, IOException, LexerException, RecognitionException {
162 final File cppResultFile =
new File( cppResultPath );
163 System.err.println(
"XXX C Parser: "+cppResultPath);
164 try(
final FileInputStream inStream =
new FileInputStream(cppResultFile) ) {
165 final DataInputStream dis =
new DataInputStream(inStream);
168 lexer.setTokenObjectClass(
CToken.class.getName());
174 parser.setASTNodeClass(
TNode.class.getName());
181 }
catch (
final RecognitionException e) {
182 throw new RuntimeException(String.format(
183 "Fatal error during translation (Localisation : %s:%s:%s)",
184 e.getFilename(), e.getLine(), e.getColumn()
186 }
catch (
final TokenStreamRecognitionException e) {
187 throw new RuntimeException(String.format(
188 "Fatal error during translation (Localisation : %s:%s:%s)",
189 e.recog.getFilename(), e.recog.getLine(), e.recog.getColumn()
191 }
catch (
final TokenStreamException e) {
192 throw new RuntimeException(
"Fatal IO error", e);
195 System.err.println(
"XXX C Header Tree Parser ...");
205 headerParser.setASTNodeClass(
TNode.class.getName());
214 String enumName = enumeration.getName();
215 if (enumName.equals(
"<anonymous>")) {
219 for (
int i = 0; i < enumeration.getNumEnumerates(); ++i) {
220 final EnumType.Enumerator enumerate = enumeration.getEnum(i);
223 enumerate.getNumber(),
224 enumName, enumeration.getASTLocusTag());
225 System.err.println(
"XXX cc_ Enum "+enumCount+
":"+i+
" <"+cdef+
">");
232 for (
final Object elem : lexer.
getDefines()) {
236 System.err.println(
"XXX cc_ Defn "+defCount+
" <"+cdef+
">");
243 static String test10CCFileName =
null;
244 public static void main(
final String args[])
throws IOException {
245 for(
int i=0; i<args.length; ++i ) {
246 if(
"-in".equals(args[i]) ) {
247 test10CCFileName = args[++i];
250 final String tstname =
TestCParser.class.getName();
251 org.junit.runner.JUnitCore.
main(tstname);
static final boolean isAvailable
Represents a [native] constant expression, comprises the [native] expression, see getNativeExpr() and...
static boolean isConstantExpression(final String value)
Parses and provides access to the contents of .cfg files for the JavaEmitter.
Represents a #define of a literal to a value (a number represented in string form....
ASTLocusTag getASTLocusTag()
Returns this instance's ASTLocusTag, if available, otherwise returns null.
List getDefines()
Returns a list of Define objects corresponding to the preprocessor definitions seen during parsing.
void initialize(String src)
void setDebug(boolean debug)
final void translationUnit()
Class TNode is an implementation of the AST interface and adds many useful features:
static void setTokenVocabulary(final String s)
Set the token vocabulary to a tokentypes class generated by antlr.
Describes enumerated types.
Utility class for recording names of typedefs and structs.
String cpp(final String cSourceBasename, final String cSourceSuffix, final boolean debug)
void cc(final String cppResultPath, final boolean debug)
static void main(final String args[])