diff --git a/clang/lib/AST/NestedNameSpecifier.cpp b/clang/lib/AST/NestedNameSpecifier.cpp --- a/clang/lib/AST/NestedNameSpecifier.cpp +++ b/clang/lib/AST/NestedNameSpecifier.cpp @@ -439,6 +439,20 @@ // Note: the 'template' keyword is part of the TypeLoc. void *TypeData = LoadPointer(Data, Offset); TypeLoc TL(Qualifier->getAsType(), TypeData); + // For `T::template ST::x` + // `NestedNameSpecifierLoc::getLocalSourceRange()` should return `template + // ST::`. + // For any TypeLoc we can just use `TypeLoc::getBeginLoc()` to + // get the beginning of this LocalSourceRange but for dependent template + // specializations `getBeginLoc` returns the location of `T` yielding + // `T::template ST::`. + if (auto DependentTL = TL.getAs()) { + if (DependentTL.getTemplateKeywordLoc().isValid()) + return SourceRange(DependentTL.getTemplateKeywordLoc(), + LoadSourceLocation(Data, Offset + sizeof(void *))); + return SourceRange(DependentTL.getTemplateNameLoc(), + LoadSourceLocation(Data, Offset + sizeof(void *))); + } return SourceRange(TL.getBeginLoc(), LoadSourceLocation(Data, Offset + sizeof(void*))); }