From e1a6a7d6d3676db194928d0025a40fe9ea3842de Mon Sep 17 00:00:00 2001 From: Richard Trieu Date: Fri, 5 Aug 2016 03:16:36 +0000 Subject: [PATCH] Fix crash in template type diffing. When the type being diffed is a type alias, and the orginal type is not a templated type, then there will be no unsugared TemplateSpecializationType. When this happens, exit early from the constructor. Also add assertions to the other iterator accessor to prevent the iterator from being used. llvm-svn: 277797 --- clang/lib/AST/ASTDiagnostic.cpp | 6 ++++++ clang/test/Misc/diag-template-diffing.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/clang/lib/AST/ASTDiagnostic.cpp b/clang/lib/AST/ASTDiagnostic.cpp index b6b9c2f..85ab4cc 100644 --- a/clang/lib/AST/ASTDiagnostic.cpp +++ b/clang/lib/AST/ASTDiagnostic.cpp @@ -916,6 +916,8 @@ class TemplateDiff { /// template argument. InternalIterator(const TemplateSpecializationType *TST) : TST(TST), Index(0), CurrentTA(nullptr), EndTA(nullptr) { + if (!TST) return; + if (isEnd()) return; // Set to first template argument. If not a parameter pack, done. @@ -936,11 +938,13 @@ class TemplateDiff { /// isEnd - Returns true if the iterator is one past the end. bool isEnd() const { + assert(TST && "InternalalIterator is invalid with a null TST."); return Index >= TST->getNumArgs(); } /// &operator++ - Increment the iterator to the next template argument. InternalIterator &operator++() { + assert(TST && "InternalalIterator is invalid with a null TST."); if (isEnd()) { return *this; } @@ -976,6 +980,7 @@ class TemplateDiff { /// operator* - Returns the appropriate TemplateArgument. reference operator*() const { + assert(TST && "InternalalIterator is invalid with a null TST."); assert(!isEnd() && "Index exceeds number of arguments."); if (CurrentTA == EndTA) return TST->getArg(Index); @@ -985,6 +990,7 @@ class TemplateDiff { /// operator-> - Allow access to the underlying TemplateArgument. pointer operator->() const { + assert(TST && "InternalalIterator is invalid with a null TST."); return &operator*(); } }; diff --git a/clang/test/Misc/diag-template-diffing.cpp b/clang/test/Misc/diag-template-diffing.cpp index 90bcf6b..a4f29cc 100644 --- a/clang/test/Misc/diag-template-diffing.cpp +++ b/clang/test/Misc/diag-template-diffing.cpp @@ -1455,7 +1455,37 @@ void run() { } // CHECK-ELIDE-NOTREE: error: no matching function for call to 'D' // CHECK-ELIDE-NOTREE: note: candidate function [with x = TypeAlias::X::X1] not viable: no known conversion from 'VectorType' to 'const VectorType<(TypeAlias::X)0>' for 1st argument +} + +namespace TypeAlias2 { +template +class A {}; + +template +using A_reg = A; +void take_reg(A_reg); + +template +using A_ptr = A *; +void take_ptr(A_ptr); + +template +using A_ref = const A &; +void take_ref(A_ref); +void run(A_reg reg, A_ptr ptr, A_ref ref) { + take_reg(reg); +// CHECK-ELIDE-NOTREE: error: no matching function for call to 'take_reg' +// CHECK-ELIDE-NOTREE: note: candidate function not viable: no known conversion from 'A_reg' to 'A_reg' for 1st argument + + take_ptr(ptr); +// CHECK-ELIDE-NOTREE: error: no matching function for call to 'take_ptr' +// CHECK-ELIDE-NOTREE: note: candidate function not viable: no known conversion from 'A_ptr' to 'A_ptr' for 1st argument + + take_ref(ref); +// CHECK-ELIDE-NOTREE: error: no matching function for call to 'take_ref' +// CHECK-ELIDE-NOTREE: note: candidate function not viable: no known conversion from 'const A' to 'const A' for 1st argument +} } // CHECK-ELIDE-NOTREE: {{[0-9]*}} errors generated. -- 2.7.4