When destroying a translation unit, deallocate its owned declarations in reverse order, because there may be dependencies among the declarations.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@58244 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h
index be8d842..e1e34f4 100644
--- a/include/clang/AST/Decl.h
+++ b/include/clang/AST/Decl.h
@@ -514,23 +514,10 @@
   typedef ParmVarDecl * const *param_const_iterator;
   
   param_iterator param_begin() { return ParamInfo; }
-  param_iterator param_end() {
-
-    // Special-case for handling typedefs:
-    //
-    //  typedef void func_t(int x);
-    //  func_t a;
-    //
-    // In the case of the FunctionDecl for "a", there are no ParmVarDecls.
-
-    return ParamInfo ? ParamInfo+param_size() : 0x0;
-  }
+  param_iterator param_end()   { return ParamInfo+param_size(); }
   
   param_const_iterator param_begin() const { return ParamInfo; }
-  
-  param_const_iterator param_end() const {
-    return ParamInfo ? ParamInfo+param_size() : 0x0;
-  }
+  param_const_iterator param_end() const   { return ParamInfo+param_size(); }
   
   unsigned getNumParams() const;
   const ParmVarDecl *getParamDecl(unsigned i) const {
diff --git a/lib/AST/TranslationUnit.cpp b/lib/AST/TranslationUnit.cpp
index 659aa06..42b7cea 100644
--- a/lib/AST/TranslationUnit.cpp
+++ b/lib/AST/TranslationUnit.cpp
@@ -33,7 +33,9 @@
 TranslationUnit::~TranslationUnit() {
   if (OwnsDecls) {
     llvm::DenseSet<Decl*> Killed;
-    for (iterator I=begin(), E=end(); I!=E; ++I) {
+    for (std::vector<Decl*>::reverse_iterator I=TopLevelDecls.rbegin(), 
+                                              E=TopLevelDecls.rend(); 
+         I!=E; ++I) {
       if (Killed.count(*I)) continue;
 
       Killed.insert(*I);
diff --git a/test/SemaCXX/fntype-decl.cpp b/test/SemaCXX/fntype-decl.cpp
index b543466..d9fb8bb 100644
--- a/test/SemaCXX/fntype-decl.cpp
+++ b/test/SemaCXX/fntype-decl.cpp
@@ -3,3 +3,13 @@
 // PR2942
 typedef void fn(int);
 fn f;
+
+int g(int x, int y);
+int g(int x, int y = 2);
+
+typedef int g_type(int, int);
+g_type g;
+
+int h(int x) {
+  return g(x);
+}