Skip to content

Commit 6efb7d2

Browse files
committed
Merge from 'master' to 'sycl-web' (#13)
CONFLICT (content): Merge conflict in clang/test/SemaSYCL/kernel-attribute.cpp CONFLICT (content): Merge conflict in clang/test/Preprocessor/sycl-macro.cpp CONFLICT (content): Merge conflict in clang/test/Driver/sycl.c CONFLICT (content): Merge conflict in clang/lib/Frontend/InitPreprocessor.cpp CONFLICT (content): Merge conflict in clang/lib/Frontend/CompilerInvocation.cpp CONFLICT (content): Merge conflict in clang/lib/Driver/ToolChains/Clang.cpp CONFLICT (content): Merge conflict in clang/include/clang/Driver/Options.td CONFLICT (content): Merge conflict in clang/include/clang/Basic/LangOptions.def
2 parents b524502 + bd97704 commit 6efb7d2

File tree

283 files changed

+7689
-2575
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

283 files changed

+7689
-2575
lines changed

clang-tools-extra/clangd/AST.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "clang/Basic/SourceManager.h"
2626
#include "clang/Basic/Specifiers.h"
2727
#include "clang/Index/USRGeneration.h"
28-
#include "clang/Lex/Lexer.h"
2928
#include "llvm/ADT/ArrayRef.h"
3029
#include "llvm/ADT/Optional.h"
3130
#include "llvm/ADT/STLExtras.h"
@@ -417,16 +416,8 @@ class DeducedTypeVisitor : public RecursiveASTVisitor<DeducedTypeVisitor> {
417416

418417
llvm::Optional<QualType> getDeducedType(ASTContext &ASTCtx,
419418
SourceLocation Loc) {
420-
Token Tok;
421-
// Only try to find a deduced type if the token is auto or decltype.
422-
if (!Loc.isValid() ||
423-
Lexer::getRawToken(Loc, Tok, ASTCtx.getSourceManager(),
424-
ASTCtx.getLangOpts(), false) ||
425-
!Tok.is(tok::raw_identifier) ||
426-
!(Tok.getRawIdentifier() == "auto" ||
427-
Tok.getRawIdentifier() == "decltype")) {
419+
if (!Loc.isValid())
428420
return {};
429-
}
430421
DeducedTypeVisitor V(Loc);
431422
V.TraverseAST(ASTCtx);
432423
if (V.DeducedType.isNull())

clang-tools-extra/clangd/AST.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ NestedNameSpecifierLoc getQualifierLoc(const NamedDecl &ND);
109109
QualType declaredType(const TypeDecl *D);
110110

111111
/// Retrieves the deduced type at a given location (auto, decltype).
112-
/// Retuns None unless Loc starts an auto/decltype token.
113112
/// It will return the underlying type.
114113
llvm::Optional<QualType> getDeducedType(ASTContext &, SourceLocation Loc);
115114

clang-tools-extra/clangd/Hover.cpp

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
#include "clang/AST/PrettyPrinter.h"
2828
#include "clang/AST/Type.h"
2929
#include "clang/Basic/Specifiers.h"
30+
#include "clang/Basic/TokenKinds.h"
3031
#include "clang/Index/IndexSymbol.h"
32+
#include "clang/Tooling/Syntax/Tokens.h"
3133
#include "llvm/ADT/None.h"
3234
#include "llvm/ADT/Optional.h"
3335
#include "llvm/ADT/STLExtras.h"
@@ -523,24 +525,44 @@ llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
523525
format::FormatStyle Style,
524526
const SymbolIndex *Index) {
525527
const SourceManager &SM = AST.getSourceManager();
526-
llvm::Optional<HoverInfo> HI;
527-
SourceLocation SourceLocationBeg = SM.getMacroArgExpandedLocation(
528-
getBeginningOfIdentifier(Pos, SM, AST.getLangOpts()));
528+
auto CurLoc = sourceLocationInMainFile(SM, Pos);
529+
if (!CurLoc) {
530+
llvm::consumeError(CurLoc.takeError());
531+
return llvm::None;
532+
}
533+
auto TokensTouchingCursor =
534+
syntax::spelledTokensTouching(*CurLoc, AST.getTokens());
535+
if (TokensTouchingCursor.empty())
536+
return llvm::None;
529537

530-
if (auto Deduced = getDeducedType(AST.getASTContext(), SourceLocationBeg)) {
538+
// In general we prefer the touching token that works over the one that
539+
// doesn't, see SelectionTree::create(). The following locations are used only
540+
// for triggering on macros and auto/decltype, so simply choosing the lone
541+
// identifier-or-keyword token is equivalent.
542+
SourceLocation IdentLoc;
543+
SourceLocation AutoLoc;
544+
for (const auto &Tok : TokensTouchingCursor) {
545+
if (Tok.kind() == tok::identifier)
546+
IdentLoc = Tok.location();
547+
if (Tok.kind() == tok::kw_auto || Tok.kind() == tok::kw_decltype)
548+
AutoLoc = Tok.location();
549+
}
550+
551+
llvm::Optional<HoverInfo> HI;
552+
if (auto Deduced = getDeducedType(AST.getASTContext(), AutoLoc)) {
531553
HI = getHoverContents(*Deduced, AST.getASTContext(), Index);
532-
} else if (auto M = locateMacroAt(SourceLocationBeg, AST.getPreprocessor())) {
554+
HI->SymRange =
555+
getTokenRange(AST.getSourceManager(), AST.getLangOpts(), AutoLoc);
556+
} else if (auto M = locateMacroAt(IdentLoc, AST.getPreprocessor())) {
533557
HI = getHoverContents(*M, AST);
558+
HI->SymRange =
559+
getTokenRange(AST.getSourceManager(), AST.getLangOpts(), IdentLoc);
534560
} else {
535-
auto Offset = positionToOffset(SM.getBufferData(SM.getMainFileID()), Pos);
536-
if (!Offset) {
537-
llvm::consumeError(Offset.takeError());
538-
return llvm::None;
539-
}
561+
auto Offset = SM.getFileOffset(*CurLoc);
540562
// Editors send the position on the left of the hovered character.
541563
// So our selection tree should be biased right. (Tested with VSCode).
542564
SelectionTree ST = SelectionTree::createRight(
543-
AST.getASTContext(), AST.getTokens(), *Offset, *Offset);
565+
AST.getASTContext(), AST.getTokens(), Offset, Offset);
544566
std::vector<const Decl *> Result;
545567
if (const SelectionTree::Node *N = ST.commonAncestor()) {
546568
auto Decls = explicitReferenceTargets(N->ASTNode, DeclRelation::Alias);
@@ -565,9 +587,15 @@ llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
565587
if (auto Formatted =
566588
tooling::applyAllReplacements(HI->Definition, Replacements))
567589
HI->Definition = *Formatted;
590+
// FIXME: We should rather fill this with info coming from SelectionTree node.
591+
if (!HI->SymRange) {
592+
SourceLocation ToHighlight = TokensTouchingCursor.front().location();
593+
if (IdentLoc.isValid())
594+
ToHighlight = IdentLoc;
595+
HI->SymRange =
596+
getTokenRange(AST.getSourceManager(), AST.getLangOpts(), ToHighlight);
597+
}
568598

569-
HI->SymRange = getTokenRange(AST.getSourceManager(), AST.getLangOpts(),
570-
SourceLocationBeg);
571599
return HI;
572600
}
573601

clang-tools-extra/clangd/SourceCode.cpp

Lines changed: 0 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
340238
bool isValidFileRange(const SourceManager &Mgr, SourceRange R) {
341239
if (!R.getBegin().isValid() || !R.getEnd().isValid())
342240
return false;

clang-tools-extra/clangd/SourceCode.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,6 @@ llvm::Optional<Range> getTokenRange(const SourceManager &SM,
7878
llvm::Expected<SourceLocation> sourceLocationInMainFile(const SourceManager &SM,
7979
Position P);
8080

81-
/// Get the beginning SourceLocation at a specified \p Pos in the main file.
82-
/// May be invalid if Pos is, or if there's no identifier or operators.
83-
/// The returned position is in the main file, callers may prefer to
84-
/// obtain the macro expansion location.
85-
SourceLocation getBeginningOfIdentifier(const Position &Pos,
86-
const SourceManager &SM,
87-
const LangOptions &LangOpts);
88-
8981
/// Returns true iff \p Loc is inside the main file. This function handles
9082
/// file & macro locations. For macro locations, returns iff the macro is being
9183
/// expanded inside the main file.

clang-tools-extra/clangd/refactor/Rename.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,9 @@ llvm::Expected<FileEdits> rename(const RenameInputs &RInputs) {
531531
if (!MainFileRenameEdit)
532532
return MainFileRenameEdit.takeError();
533533

534-
if (!Opts.AllowCrossFile) {
535-
// Within-file rename: just return the main file results.
534+
// return the main file edit if this is a within-file rename or the symbol
535+
// being renamed is function local.
536+
if (!Opts.AllowCrossFile || RenameDecl.getParentFunctionOrMethod()) {
536537
return FileEdits(
537538
{std::make_pair(RInputs.MainFilePath,
538539
Edit{MainFileCode, std::move(*MainFileRenameEdit)})});

clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ TEST(CollectMainFileMacros, SelectedMacros) {
8787
if (ExpectedRefs.empty())
8888
break;
8989

90-
auto Loc = getBeginningOfIdentifier(ExpectedRefs.begin()->start, SM,
91-
AST.getLangOpts());
92-
auto Macro = locateMacroAt(Loc, PP);
90+
auto Loc = sourceLocationInMainFile(SM, ExpectedRefs.begin()->start);
91+
ASSERT_TRUE(bool(Loc));
92+
auto Macro = locateMacroAt(*Loc, PP);
9393
assert(Macro);
9494
auto SID = getSymbolID(Macro->Name, Macro->Info, SM);
9595

clang-tools-extra/clangd/unittests/SourceCodeTests.cpp

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -312,60 +312,6 @@ TEST(SourceCodeTests, SourceLocationInMainFile) {
312312
}
313313
}
314314

315-
TEST(SourceCodeTests, GetBeginningOfIdentifier) {
316-
std::string Preamble = R"cpp(
317-
struct Bar { int func(); };
318-
#define MACRO(X) void f() { X; }
319-
Bar* bar;
320-
)cpp";
321-
// First ^ is the expected beginning, last is the search position.
322-
for (const std::string &Text : std::vector<std::string>{
323-
"int ^f^oo();", // inside identifier
324-
"int ^foo();", // beginning of identifier
325-
"int ^foo^();", // end of identifier
326-
"int foo(^);", // non-identifier
327-
"^int foo();", // beginning of file (can't back up)
328-
"int ^f0^0();", // after a digit (lexing at N-1 is wrong)
329-
"/^/ comments", // non-interesting token
330-
"void f(int abc) { abc ^ ++; }", // whitespace
331-
"void f(int abc) { ^abc^++; }", // range of identifier
332-
"void f(int abc) { ++^abc^; }", // range of identifier
333-
"void f(int abc) { ++^abc; }", // range of identifier
334-
"void f(int abc) { ^+^+abc; }", // range of operator
335-
"void f(int abc) { ^abc^ ++; }", // range of identifier
336-
"void f(int abc) { abc ^++^; }", // range of operator
337-
"void f(int abc) { ^++^ abc; }", // range of operator
338-
"void f(int abc) { ++ ^abc^; }", // range of identifier
339-
"void f(int abc) { ^++^/**/abc; }", // range of operator
340-
"void f(int abc) { ++/**/^abc; }", // range of identifier
341-
"void f(int abc) { ^abc^/**/++; }", // range of identifier
342-
"void f(int abc) { abc/**/^++; }", // range of operator
343-
"void f() {^ }", // outside of identifier and operator
344-
"int ^λλ^λ();", // UTF-8 handled properly when backing up
345-
346-
// identifier in macro arg
347-
"MACRO(bar->^func())", // beginning of identifier
348-
"MACRO(bar->^fun^c())", // inside identifier
349-
"MACRO(bar->^func^())", // end of identifier
350-
"MACRO(^bar->func())", // begin identifier
351-
"MACRO(^bar^->func())", // end identifier
352-
"^MACRO(bar->func())", // beginning of macro name
353-
"^MAC^RO(bar->func())", // inside macro name
354-
"^MACRO^(bar->func())", // end of macro name
355-
}) {
356-
std::string WithPreamble = Preamble + Text;
357-
Annotations TestCase(WithPreamble);
358-
auto AST = TestTU::withCode(TestCase.code()).build();
359-
const auto &SourceMgr = AST.getSourceManager();
360-
SourceLocation Actual = getBeginningOfIdentifier(
361-
TestCase.points().back(), SourceMgr, AST.getLangOpts());
362-
Position ActualPos = offsetToPosition(
363-
TestCase.code(),
364-
SourceMgr.getFileOffset(SourceMgr.getSpellingLoc(Actual)));
365-
EXPECT_EQ(TestCase.points().front(), ActualPos) << Text;
366-
}
367-
}
368-
369315
TEST(SourceCodeTests, CollectIdentifiers) {
370316
auto Style = format::getLLVMStyle();
371317
auto IDs = collectIdentifiers(R"cpp(
@@ -481,9 +427,11 @@ TEST(SourceCodeTests, GetMacros) {
481427
)cpp");
482428
TestTU TU = TestTU::withCode(Code.code());
483429
auto AST = TU.build();
484-
auto Loc = getBeginningOfIdentifier(Code.point(), AST.getSourceManager(),
485-
AST.getLangOpts());
486-
auto Result = locateMacroAt(Loc, AST.getPreprocessor());
430+
auto CurLoc = sourceLocationInMainFile(AST.getSourceManager(), Code.point());
431+
ASSERT_TRUE(bool(CurLoc));
432+
const auto *Id = syntax::spelledIdentifierTouching(*CurLoc, AST.getTokens());
433+
ASSERT_TRUE(Id);
434+
auto Result = locateMacroAt(Id->location(), AST.getPreprocessor());
487435
ASSERT_TRUE(Result);
488436
EXPECT_THAT(*Result, MacroName("MACRO"));
489437
}

0 commit comments

Comments
 (0)