Re-commit r183466 with a fix to make the TypeLoc casting machinery work
correctly in the presence of qualified types.

(I had to change the unittest because it was trying to cast a
QualifiedTypeLoc to TemplateSpecializationTypeLoc.)

llvm-svn: 183563
diff --git a/clang/unittests/AST/SourceLocationTest.cpp b/clang/unittests/AST/SourceLocationTest.cpp
index 55356f1..990e6df 100644
--- a/clang/unittests/AST/SourceLocationTest.cpp
+++ b/clang/unittests/AST/SourceLocationTest.cpp
@@ -159,7 +159,7 @@
 protected:
   virtual SourceRange getRange(const TypeLoc &Node) {
     TemplateSpecializationTypeLoc T =
-        Node.castAs<TemplateSpecializationTypeLoc>();
+        Node.getUnqualifiedLoc().castAs<TemplateSpecializationTypeLoc>();
     assert(!T.isNull());
     return SourceRange(T.getLAngleLoc(), T.getRAngleLoc());
   }