@@ -123,6 +123,12 @@ static std::string unquote(const std::string &s)
123123}
124124
125125
126+ // location in a buffer that is generated by clang: "<<built-in>:115:23, file.h:18:19>"
127+ static bool isGeneratedBufferLocation (const std::string &s, std::string::size_type pos = 0 )
128+ {
129+ return s.compare (pos, 12 , " <<built-in>:" ) == 0 || s.compare (pos, 17 , " <<scratch space>:" ) == 0 ;
130+ }
131+
126132static std::vector<std::string> splitString (const std::string &line)
127133{
128134 std::vector<std::string> ret;
@@ -134,9 +140,11 @@ static std::vector<std::string> splitString(const std::string &line)
134140 pos1 = line.find_first_not_of (' ' , pos1 + 1 );
135141 continue ;
136142 }
137- if (line[pos1] == ' <' )
143+ if (line[pos1] == ' <' ) {
138144 pos2 = line.find (' >' , pos1);
139- else if (line[pos1] == ' \" ' )
145+ if (pos2 != std::string::npos && isGeneratedBufferLocation (line, pos1))
146+ pos2 = line.find (' >' , pos2 + 1 );
147+ } else if (line[pos1] == ' \" ' )
140148 pos2 = line.find (' \" ' , pos1+1 );
141149 else if (line[pos1] == ' \' ' ) {
142150 pos2 = line.find (' \' ' , pos1+1 );
@@ -560,19 +568,32 @@ void clangimport::AstNode::setLocations(TokenList &tokenList, int file, int line
560568 if (pos != std::string::npos)
561569 col = strToInt<int >(ext.substr (pos+6 , ext.find_first_of (" :,>" , pos+6 ) - (pos+6 )));
562570 } else if (ext[0 ] == ' <' ) {
563- const std::string::size_type colon = ext.find (' :' );
564- if (colon != std::string::npos) {
565- const bool windowsPath = colon == 2 && ext.size () > 3 && ext[2 ] == ' :' ;
566- const std::string::size_type sep1 = windowsPath ? ext.find (' :' , 4 ) : colon;
567- const std::string::size_type sep2 = ext.find (' :' , sep1 + 1 );
568- file = tokenList.appendFileIfNew (ext.substr (1 , sep1 - 1 ));
569- line = strToInt<int >(ext.substr (sep1 + 1 , sep2 - sep1 - 1 ));
571+ // start of the file location
572+ std::string::size_type pos = 1 ;
573+ if (isGeneratedBufferLocation (ext)) {
574+ // Use the end of the range if that is a file location, otherwise the location is unchanged
575+ pos = ext.find (" , " );
576+ if (pos != std::string::npos) {
577+ pos += 2 ;
578+ if (ext[pos] == ' <' || ext.compare (pos, 4 , " col:" ) == 0 || ext.compare (pos, 5 , " line:" ) == 0 )
579+ pos = std::string::npos;
580+ }
570581 }
571- else {
572- // "<invalid sloc>" are encountered in every AST dump by some built-in TypedefDecl
573- // an completely empty location block was encountered with a CompoundStmt
574- if (ext != " <<invalid sloc>" && ext != " <>" )
575- throw InternalError (nullptr , " invalid AST location: " + ext, InternalError::AST );
582+ if (pos != std::string::npos) {
583+ const std::string::size_type colon = ext.find (' :' , pos);
584+ if (colon != std::string::npos) {
585+ const bool windowsPath = colon == pos + 1 && ext.size () > pos + 2 ;
586+ const std::string::size_type sep1 = windowsPath ? ext.find (' :' , pos + 3 ) : colon;
587+ const std::string::size_type sep2 = ext.find (' :' , sep1 + 1 );
588+ file = tokenList.appendFileIfNew (ext.substr (pos, sep1 - pos));
589+ line = strToInt<int >(ext.substr (sep1 + 1 , sep2 - sep1 - 1 ));
590+ }
591+ else {
592+ // "<invalid sloc>" are encountered in every AST dump by some built-in TypedefDecl
593+ // an completely empty location block was encountered with a CompoundStmt
594+ if (ext != " <<invalid sloc>" && ext != " <>" )
595+ throw InternalError (nullptr , " invalid AST location: " + ext, InternalError::AST );
596+ }
576597 }
577598 }
578599 }
0 commit comments