@@ -952,9 +952,39 @@ class SemanticAnnotator : public SourceEntityWalker {
952952public:
953953
954954 std::vector<SwiftSemanticToken> SemaToks;
955+ bool IsWalkingMacroExpansionBuffer = false ;
955956
956957 SemanticAnnotator (SourceManager &SM, unsigned BufferID)
957- : SM(SM), BufferID(BufferID) {}
958+ : SM(SM), BufferID(BufferID) {
959+ if (auto GeneratedSourceInfo = SM.getGeneratedSourceInfo (BufferID)) {
960+ switch (GeneratedSourceInfo->kind ) {
961+ case GeneratedSourceInfo::ExpressionMacroExpansion:
962+ case GeneratedSourceInfo::FreestandingDeclMacroExpansion:
963+ case GeneratedSourceInfo::AccessorMacroExpansion:
964+ case GeneratedSourceInfo::MemberAttributeMacroExpansion:
965+ case GeneratedSourceInfo::MemberMacroExpansion:
966+ case GeneratedSourceInfo::PeerMacroExpansion:
967+ case GeneratedSourceInfo::ConformanceMacroExpansion:
968+ case GeneratedSourceInfo::ExtensionMacroExpansion:
969+ IsWalkingMacroExpansionBuffer = true ;
970+ break ;
971+ case GeneratedSourceInfo::ReplacedFunctionBody:
972+ case GeneratedSourceInfo::PrettyPrinted:
973+ break ;
974+ }
975+ }
976+ }
977+
978+ MacroWalking getMacroWalkingBehavior () const override {
979+ if (IsWalkingMacroExpansionBuffer) {
980+ // When we are walking a macro expansion buffer, we need to set the macro
981+ // walking behavior to walk the expansion, otherwise we skip over all the
982+ // declarations in the buffer.
983+ return MacroWalking::ArgumentsAndExpansion;
984+ } else {
985+ return SourceEntityWalker::getMacroWalkingBehavior ();
986+ }
987+ }
958988
959989 bool visitDeclReference (ValueDecl *D, CharSourceRange Range,
960990 TypeDecl *CtorTyRef, ExtensionDecl *ExtTyRef, Type T,
@@ -996,6 +1026,12 @@ class SemanticAnnotator : public SourceEntityWalker {
9961026 if (!Range.isValid ())
9971027 return ;
9981028
1029+ // If we are walking into macro expansions, make sure we only report ranges
1030+ // from the requested buffer, not any buffers of child macro expansions.
1031+ if (IsWalkingMacroExpansionBuffer &&
1032+ SM.findBufferContainingLoc (Range.getStart ()) != BufferID) {
1033+ return ;
1034+ }
9991035 unsigned ByteOffset = SM.getLocOffsetInBuffer (Range.getStart (), BufferID);
10001036 unsigned Length = Range.getByteLength ();
10011037 auto Kind = ContextFreeCodeCompletionResult::getCodeCompletionDeclKind (D);
0 commit comments