33import java .io .IOException ;
44import java .io .PrintWriter ;
55import java .io .StringReader ;
6+ import java .net .URISyntaxException ;
67import java .util .UUID ;
78
89import org .eclipse .cdt .core .dom .ast .IASTFileLocation ;
@@ -26,46 +27,49 @@ public Locations(IValueFactory vf, PrintWriter stdErr) {
2627
2728 public ISourceLocation forNode (IASTNode node ) {
2829 try {
29- IASTFileLocation astFileLocation = node .getFileLocation ();
30+ IASTFileLocation astFileLocation = node .getFileLocation ();
3031
31- if (astFileLocation != null ) {
32- String fileName = astFileLocation .getFileName ();
32+ if (astFileLocation != null ) {
33+ String fileName = astFileLocation .getFileName ();
3334
34- // TODO: JV; I find this looking kind of tricky. Where would the wrong
35- // slashes come from and should we not solve the problem at the source?
36- // is there a way to get a "File" abstraction or Resource from CDT?
37- fileName = fileName .replace ('\\' , '/' );
38-
39- if (fileName .trim ().startsWith ("|" )) {
40- try {
41- ISourceLocation tmp = (ISourceLocation ) locParser .read (vf , TypeFactory .getInstance ().sourceLocationType (), new StringReader (fileName ));
35+ if (fileName .trim ().startsWith ("|" )) {
36+ // here we used Rascal loc syntax which ended up in the CDT's file path somehow
4237
43- return vf .sourceLocation (
44- tmp ,
45- astFileLocation .getNodeOffset (),
46- astFileLocation .getNodeLength ());
47- } catch (FactParseError | FactTypeUseException | IOException e ) {
48-
49- return unknownPreciseLoc ();
50- }
51- }
38+ try {
39+ ISourceLocation tmp = (ISourceLocation ) locParser .read (vf , TypeFactory .getInstance ().sourceLocationType (), new StringReader (fileName ));
40+
41+ // that would be weird, and also we'd overwrite it below
42+ assert !tmp .hasOffsetLength ();
5243
53- if (!fileName .startsWith ("/" )) {
54- fileName = "/" + fileName ;
55- }
44+ return vf .sourceLocation (
45+ tmp ,
46+ astFileLocation .getNodeOffset (),
47+ astFileLocation .getNodeLength ());
48+ }
49+ catch (FactParseError | FactTypeUseException | IOException e ) {
50+ return unknownPreciseLoc ();
51+ }
52+ }
53+ else {
54+ // this parses the fileName in an OS-ARCH dependent way
55+ ISourceLocation loc = URIUtil .createFileLocation (fileName .trim ());
5656
57- return vf .sourceLocation (
58- vf . sourceLocation ( fileName ) ,
57+ return vf .sourceLocation (
58+ loc ,
5959 astFileLocation .getNodeOffset (),
6060 astFileLocation .getNodeLength ()
6161 );
62- }
62+ }
63+ }
6364
64- return unknownPreciseLoc ();
65- }
66- catch (RuntimeException e ) {
67- throw new RuntimeException ("AST at " + node + " failed" , e );
68- }
65+ return unknownPreciseLoc ();
66+ }
67+ catch (RuntimeException e ) {
68+ throw new RuntimeException ("getting location for node " + node + " failed" , e );
69+ }
70+ catch (URISyntaxException e ) {
71+ throw new RuntimeException ("location for not " + node + " has URISyntaxException: " + e , e );
72+ }
6973 }
7074
7175 private ISourceLocation unknownPreciseLoc () {
0 commit comments