Skip to content

Commit fe649d5

Browse files
committed
added NPE diagnostics around all CdtToRascal calls
1 parent 4f346c6 commit fe649d5

File tree

1 file changed

+103
-57
lines changed

1 file changed

+103
-57
lines changed

src/lang/cpp/internal/Parser.java

Lines changed: 103 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ private void reset() {
285285
functionDefinitions = vf.setWriter();
286286
implicitDeclarations = vf.setWriter();
287287
newResolutions = vf.setWriter();
288+
currentNode = null;
288289
}
289290

290291
public IList parseFiles(IList files, IList stdLib, IList includeDirs, IMap standardMacros, IMap additionalMacros, IBool includeStdLib) {
@@ -304,11 +305,18 @@ public IList parseFiles(IList files, IList stdLib, IList includeDirs, IMap stand
304305
}
305306
ISourceLocation file = (ISourceLocation) v;
306307
IASTTranslationUnit tu = parser.parseFileAsCpp(file);
307-
IValue result = convertCdtToRascal(tu, false);
308-
ISourceLocation tuDecl = URIUtil.correctLocation("cpp+translationUnit", "", file.getPath());
309-
result = ((IConstructor) result).asWithKeywordParameters().setParameter("decl", tuDecl);
310-
asts.append(result);
308+
try {
309+
IValue result = convertCdtToRascal(tu, false);
310+
ISourceLocation tuDecl = URIUtil.correctLocation("cpp+translationUnit", "", file.getPath());
311+
result = ((IConstructor) result).asWithKeywordParameters().setParameter("decl", tuDecl);
312+
asts.append(result);
313+
}
314+
catch (NullPointerException e) {
315+
throw new UnsupportedOperationException(
316+
"Conversion to AST did not work for " + currentNode.getClass().getCanonicalName() + " at " + locs.forNode(currentNode), e);
317+
}
311318
}
319+
312320
Instant done = Instant.now();
313321
out("Parsing and marshalling " + files.size() + " files took "
314322
+ (Duration.between(begin, done).toMillis() * 1.0d) / 1000 + "seconds");
@@ -323,25 +331,27 @@ public IValue parseC(ISourceLocation file, IList stdLib, IList includeDirs, IMap
323331
this.includeStdLib = includeStdLib.getValue() || stdLib.isEmpty();
324332
this.stdLib = stdLib;
325333

326-
// Instant begin = Instant.now();
327-
// out("Beginning at " + begin.toString());
328334
CDTParser parser = new CDTParser(vf, stdOut, stdErr, stdLib, includeDirs, standardMacros, additionalMacros,
329335
includeStdLib.getValue());
330336
IASTTranslationUnit tu = parser.parseFileAsC(file);
331-
// Instant between = Instant.now();
332-
// out("CDT took " + new Double(Duration.between(begin, between).toMillis()).doubleValue() / 1000 + "seconds");
333-
IValue result = convertCdtToRascal(tu, false);
334-
ISourceLocation tuDecl = URIUtil.correctLocation("cpp+translationUnit", "", file.getPath());
335-
result = ((IConstructor) result).asWithKeywordParameters().setParameter("decl", tuDecl);
336-
// Instant done = Instant.now();
337-
// out("Marshalling took " + new Double(Duration.between(between, done).toMillis()).doubleValue() / 1000
338-
// + "seconds");
339-
reset();
340-
if (result == null) {
341-
throw RuntimeExceptionFactory.parseError(file, null, null);
342-
}
343-
return result;
344337

338+
try {
339+
IValue result = convertCdtToRascal(tu, false);
340+
ISourceLocation tuDecl = URIUtil.correctLocation("cpp+translationUnit", "", file.getPath());
341+
result = ((IConstructor) result).asWithKeywordParameters().setParameter("decl", tuDecl);
342+
343+
if (result == null) {
344+
throw RuntimeExceptionFactory.parseError(file, null, null);
345+
}
346+
return result;
347+
}
348+
catch (NullPointerException e) {
349+
throw new UnsupportedOperationException(
350+
"Conversion to AST did not work for " + currentNode.getClass().getCanonicalName() + " at " + locs.forNode(currentNode), e);
351+
}
352+
finally {
353+
reset();
354+
}
345355
}
346356

347357
public IValue parseCpp(ISourceLocation file, IList stdLib, IList includeDirs, IMap standardMacros, IMap additionalMacros,
@@ -356,17 +366,27 @@ public IValue parseCpp(ISourceLocation file, IList stdLib, IList includeDirs, IM
356366
IASTTranslationUnit tu = parser.parseFileAsCpp(file);
357367
Instant between = Instant.now();
358368
out("CDT took " + (Duration.between(begin, between).toMillis() * 1.0d) / 1000 + "seconds");
359-
IValue result = convertCdtToRascal(tu, false);
360-
ISourceLocation tuDecl = URIUtil.correctLocation("cpp+translationUnit", "", file.getPath());
361-
result = ((IConstructor) result).asWithKeywordParameters().setParameter("decl", tuDecl);
362-
Instant done = Instant.now();
363-
out("Marshalling took " + (Duration.between(between, done).toMillis() * 1.0d) / 1000
364-
+ "seconds");
365-
reset();
366-
if (result == null) {
367-
throw RuntimeExceptionFactory.parseError(file, null, null);
369+
370+
try {
371+
IValue result = convertCdtToRascal(tu, false);
372+
ISourceLocation tuDecl = URIUtil.correctLocation("cpp+translationUnit", "", file.getPath());
373+
result = ((IConstructor) result).asWithKeywordParameters().setParameter("decl", tuDecl);
374+
Instant done = Instant.now();
375+
out("Marshalling took " + (Duration.between(between, done).toMillis() * 1.0d) / 1000
376+
+ "seconds");
377+
378+
if (result == null) {
379+
throw RuntimeExceptionFactory.parseError(file, null, null);
380+
}
381+
return result;
382+
}
383+
catch (NullPointerException e) {
384+
throw new UnsupportedOperationException(
385+
"Conversion to AST did not work for " + currentNode.getClass().getCanonicalName() + " at " + locs.forNode(currentNode), e);
386+
}
387+
finally {
388+
reset();
368389
}
369-
return result;
370390
}
371391

372392
public ITuple parseCToM3AndAst(ISourceLocation file, IList stdLib, IList includeDirs, IMap standardMacros, IMap additionalMacros,
@@ -402,20 +422,29 @@ public ITuple parseCToM3AndAst(ISourceLocation file, IList stdLib, IList include
402422
functionDefinitions = vf.setWriter();
403423
implicitDeclarations = vf.setWriter();
404424
newResolutions = vf.setWriter();
405-
IValue result = convertCdtToRascal(tu, true);
406425

407-
// based on side-effects of the conversion
408-
ISet methodOverrides = getMethodOverrides(tu, newResolutions.done());
409-
m3 = m3.asWithKeywordParameters().setParameter("methodOverrides", methodOverrides);
426+
try {
427+
IValue result = convertCdtToRascal(tu, true);
410428

411-
result = ((IConstructor) result).asWithKeywordParameters().setParameter("decl", tuDecl);
412-
m3 = m3.asWithKeywordParameters().setParameter("declaredType", declaredType.done());
413-
m3 = m3.asWithKeywordParameters().setParameter("functionDefinitions", functionDefinitions.done());
414-
m3 = m3.asWithKeywordParameters().setParameter("containment", br.getContainmentRelation());
415-
m3 = m3.asWithKeywordParameters().setParameter("implicitDeclarations", implicitDeclarations.done());
429+
// based on side-effects of the conversion
430+
ISet methodOverrides = getMethodOverrides(tu, newResolutions.done());
431+
m3 = m3.asWithKeywordParameters().setParameter("methodOverrides", methodOverrides);
416432

417-
reset();
418-
return vf.tuple(m3, result);
433+
result = ((IConstructor) result).asWithKeywordParameters().setParameter("decl", tuDecl);
434+
m3 = m3.asWithKeywordParameters().setParameter("declaredType", declaredType.done());
435+
m3 = m3.asWithKeywordParameters().setParameter("functionDefinitions", functionDefinitions.done());
436+
m3 = m3.asWithKeywordParameters().setParameter("containment", br.getContainmentRelation());
437+
m3 = m3.asWithKeywordParameters().setParameter("implicitDeclarations", implicitDeclarations.done());
438+
439+
return vf.tuple(m3, result);
440+
}
441+
catch (NullPointerException e) {
442+
throw new UnsupportedOperationException(
443+
"Conversion to AST did not work for " + currentNode.getClass().getCanonicalName() + " at " + locs.forNode(currentNode), e);
444+
}
445+
finally {
446+
reset();
447+
}
419448
}
420449

421450
public ITuple parseCppToM3AndAst(ISourceLocation file, IList stdLib, IList includeDirs, IMap standardMacros, IMap additionalMacros,
@@ -442,20 +471,29 @@ public ITuple parseCppToM3AndAst(ISourceLocation file, IList stdLib, IList inclu
442471
declaredType = vf.setWriter();
443472
functionDefinitions = vf.setWriter();
444473
newResolutions = vf.setWriter();
445-
IValue result = convertCdtToRascal(tu, true);
446474

447-
// based on side-effects from the conversion
448-
ISet methodOverrides = getMethodOverrides(tu, newResolutions.done());
449-
m3 = m3.asWithKeywordParameters().setParameter("methodOverrides", methodOverrides);
450-
451-
result = ((IConstructor) result).asWithKeywordParameters().setParameter("decl", tuDecl);
452-
m3 = m3.asWithKeywordParameters().setParameter("declaredType", declaredType.done());
453-
m3 = m3.asWithKeywordParameters().setParameter("functionDefinitions", functionDefinitions.done());
454-
m3 = m3.asWithKeywordParameters().setParameter("containment", br.getContainmentRelation());
455-
m3 = m3.asWithKeywordParameters().setParameter("implicitDeclarations", implicitDeclarations.done());
475+
try {
476+
IValue result = convertCdtToRascal(tu, true);
456477

457-
reset();
458-
return vf.tuple(m3, result);
478+
// based on side-effects from the conversion
479+
ISet methodOverrides = getMethodOverrides(tu, newResolutions.done());
480+
m3 = m3.asWithKeywordParameters().setParameter("methodOverrides", methodOverrides);
481+
482+
result = ((IConstructor) result).asWithKeywordParameters().setParameter("decl", tuDecl);
483+
m3 = m3.asWithKeywordParameters().setParameter("declaredType", declaredType.done());
484+
m3 = m3.asWithKeywordParameters().setParameter("functionDefinitions", functionDefinitions.done());
485+
m3 = m3.asWithKeywordParameters().setParameter("containment", br.getContainmentRelation());
486+
m3 = m3.asWithKeywordParameters().setParameter("implicitDeclarations", implicitDeclarations.done());
487+
488+
return vf.tuple(m3, result);
489+
}
490+
catch (NullPointerException e) {
491+
throw new UnsupportedOperationException(
492+
"Conversion to AST did not work for " + currentNode.getClass().getCanonicalName() + " at " + locs.forNode(currentNode), e);
493+
}
494+
finally {
495+
reset();
496+
}
459497
}
460498

461499
public ISet getMethodOverrides(IASTTranslationUnit tu, ISet newResolutions) {
@@ -570,11 +608,19 @@ public IValue parseString(IString code, ISourceLocation loc) throws CoreExceptio
570608
int options = ILanguage.OPTION_PARSE_INACTIVE_CODE;
571609
IParserLogService log = new DefaultLogService();
572610
IASTTranslationUnit tu = GPPLanguage.getDefault().getASTTranslationUnit(fc, si, ifcp, null, options, log);
573-
IValue result = convertCdtToRascal(tu, false);
574-
ISourceLocation tuDecl = URIUtil.correctLocation("cpp+translationUnit", "", loc == null ? "" : loc.getPath());
575-
result = ((IConstructor) result).asWithKeywordParameters().setParameter("decl", tuDecl);
576-
reset();
577-
return result;
611+
612+
try {
613+
IValue result = convertCdtToRascal(tu, false);
614+
ISourceLocation tuDecl = URIUtil.correctLocation("cpp+translationUnit", "", loc == null ? "" : loc.getPath());
615+
return ((IConstructor) result).asWithKeywordParameters().setParameter("decl", tuDecl);
616+
}
617+
catch (NullPointerException e) {
618+
throw new UnsupportedOperationException(
619+
"Conversion to AST did not work for " + currentNode.getClass().getCanonicalName() + " at " + locs.forNode(currentNode), e);
620+
}
621+
finally {
622+
reset();
623+
}
578624
}
579625

580626
public IValue convertCdtToRascal(IASTTranslationUnit translationUnit, boolean toM3) {

0 commit comments

Comments
 (0)