Index: lib/Parse/ParseDecl.cpp =================================================================== --- lib/Parse/ParseDecl.cpp +++ lib/Parse/ParseDecl.cpp @@ -6227,7 +6227,16 @@ SmallVector DeclsInPrototype; if (getCurScope()->getFlags() & Scope::FunctionDeclarationScope && !getLangOpts().CPlusPlus) { - for (Decl *D : getCurScope()->decls()) { + // The decls in the scope are in arbitrary order. Add them in sorted order + // now and allow for the declarator chunk to always contain the decls in + // deterministic order. This is necessary because ActOnFunctionDeclarator + // copies the declarator chunk as is when populating the decl context, + // which could be later serialized for modules or PCHs. + SmallVector SortedDecls(getCurScope()->decls()); + llvm::sort(SortedDecls, [](const Decl *L, const Decl *R) { + return L->getID() < R->getID(); + }); + for (Decl *D : SortedDecls) { NamedDecl *ND = dyn_cast(D); if (!ND || isa(ND)) continue;