@@ -235,108 +235,6 @@ llvm::Optional<Range> getTokenRange(const SourceManager &SM,
235235 return halfOpenToRange (SM, CharSourceRange::getCharRange (TokLoc, End));
236236}
237237
238- namespace {
239-
240- enum TokenFlavor { Identifier, Operator, Whitespace, Other };
241-
242- bool isOverloadedOperator (const Token &Tok) {
243- switch (Tok.getKind ()) {
244- #define OVERLOADED_OPERATOR (Name, Spelling, Token, Unary, Binary, MemOnly ) \
245- case tok::Token:
246- #define OVERLOADED_OPERATOR_MULTI (Name, Spelling, Unary, Binary, MemOnly )
247- #include " clang/Basic/OperatorKinds.def"
248- return true ;
249-
250- default :
251- break ;
252- }
253- return false ;
254- }
255-
256- TokenFlavor getTokenFlavor (SourceLocation Loc, const SourceManager &SM,
257- const LangOptions &LangOpts) {
258- Token Tok;
259- Tok.setKind (tok::NUM_TOKENS);
260- if (Lexer::getRawToken (Loc, Tok, SM, LangOpts,
261- /* IgnoreWhiteSpace*/ false ))
262- return Other;
263-
264- // getRawToken will return false without setting Tok when the token is
265- // whitespace, so if the flag is not set, we are sure this is a whitespace.
266- if (Tok.is (tok::TokenKind::NUM_TOKENS))
267- return Whitespace;
268- if (Tok.is (tok::TokenKind::raw_identifier))
269- return Identifier;
270- if (isOverloadedOperator (Tok))
271- return Operator;
272- return Other;
273- }
274-
275- } // namespace
276-
277- SourceLocation getBeginningOfIdentifier (const Position &Pos,
278- const SourceManager &SM,
279- const LangOptions &LangOpts) {
280- FileID FID = SM.getMainFileID ();
281- auto Offset = positionToOffset (SM.getBufferData (FID), Pos);
282- if (!Offset) {
283- log (" getBeginningOfIdentifier: {0}" , Offset.takeError ());
284- return SourceLocation ();
285- }
286-
287- // GetBeginningOfToken(InputLoc) is almost what we want, but does the wrong
288- // thing if the cursor is at the end of the token (identifier or operator).
289- // The cases are:
290- // 1) at the beginning of the token
291- // 2) at the middle of the token
292- // 3) at the end of the token
293- // 4) anywhere outside the identifier or operator
294- // To distinguish all cases, we lex both at the
295- // GetBeginningOfToken(InputLoc-1) and GetBeginningOfToken(InputLoc), for
296- // cases 1 and 4, we just return the original location.
297- SourceLocation InputLoc = SM.getComposedLoc (FID, *Offset);
298- if (*Offset == 0 ) // Case 1 or 4.
299- return InputLoc;
300- SourceLocation Before = SM.getComposedLoc (FID, *Offset - 1 );
301- SourceLocation BeforeTokBeginning =
302- Lexer::GetBeginningOfToken (Before, SM, LangOpts);
303- TokenFlavor BeforeKind = getTokenFlavor (BeforeTokBeginning, SM, LangOpts);
304-
305- SourceLocation CurrentTokBeginning =
306- Lexer::GetBeginningOfToken (InputLoc, SM, LangOpts);
307- TokenFlavor CurrentKind = getTokenFlavor (CurrentTokBeginning, SM, LangOpts);
308-
309- // At the middle of the token.
310- if (BeforeTokBeginning == CurrentTokBeginning) {
311- // For interesting token, we return the beginning of the token.
312- if (CurrentKind == Identifier || CurrentKind == Operator)
313- return CurrentTokBeginning;
314- // otherwise, we return the original loc.
315- return InputLoc;
316- }
317-
318- // Whitespace is not interesting.
319- if (BeforeKind == Whitespace)
320- return CurrentTokBeginning;
321- if (CurrentKind == Whitespace)
322- return BeforeTokBeginning;
323-
324- // The cursor is at the token boundary, e.g. "Before^Current", we prefer
325- // identifiers to other tokens.
326- if (CurrentKind == Identifier)
327- return CurrentTokBeginning;
328- if (BeforeKind == Identifier)
329- return BeforeTokBeginning;
330- // Then prefer overloaded operators to other tokens.
331- if (CurrentKind == Operator)
332- return CurrentTokBeginning;
333- if (BeforeKind == Operator)
334- return BeforeTokBeginning;
335-
336- // Non-interesting case, we just return the original location.
337- return InputLoc;
338- }
339-
340238bool isValidFileRange (const SourceManager &Mgr, SourceRange R) {
341239 if (!R.getBegin ().isValid () || !R.getEnd ().isValid ())
342240 return false ;
0 commit comments