Merge pull request #11107 from jckarter/dynamic-init-breakage

SILGen: The allocator entry point for an initializer is never `dynamic`.
diff --git a/docs/Branches.md b/docs/Branches.md
index ffb8321..f5ac184 100644
--- a/docs/Branches.md
+++ b/docs/Branches.md
@@ -37,9 +37,9 @@
 
 `upstream-with-swift` is a branch for LLVM that includes all changes necessary to support Swift. Changes from llvm.org's master branch are automatically merged in. Why isn't this just `stable`? Well, because LLVM changes *very* rapidly, and that wouldn't be very stable. However, we do want to make sure the Swift stuff keeps working.
 
-If you are making changes to LLVM to support Swift, you'll probably need to work on them in `stable` to test them against Swift itself, but they should be committed to `upstream-with-swift`, and possibly cherry-picked to the current release branch (`swift-x.y-branch`) if needed.
+If you are making changes to LLVM to support Swift, you'll probably need to work on them in `stable` to test them against Swift itself, but they should be committed to `upstream-with-swift`, and cherry-picked to the current release branch (`swift-x.y-branch`) if needed. Remember, the release branches are automerged into `stable` on a regular basis.
 
-(If you're making changes to LLVM or LLDB that *aren't* about Swift, they should generally be made on llvm.org instead.)
+(If you're making changes to LLVM or LLDB that *aren't* about Swift, they should generally be made on llvm.org instead, then cherry-picked to the active release branch or `stable`.)
 
 `master-next` is an effort to keep Swift building with the latest LLVM changes. Ideally when LLVM changes, no Swift updates are needed, but that isn't always the case. In these situations, any adjustments can go into Swift's `master-next` branch. Changes from Swift's `master` are automatically merged into `master-next` as well.
 
diff --git a/include/swift/IDE/Utils.h b/include/swift/IDE/Utils.h
index 7f2ba5e..7c92366 100644
--- a/include/swift/IDE/Utils.h
+++ b/include/swift/IDE/Utils.h
@@ -48,6 +48,7 @@
   class DeclContext;
   class ClangNode;
   class ClangImporter;
+  class Token;
 
 namespace ide {
 struct SourceCompleteResult {
@@ -270,7 +271,8 @@
 struct ResolvedRangeInfo {
   RangeKind Kind;
   ReturnInfo ExitInfo;
-  CharSourceRange Content;
+  ArrayRef<Token> TokensInRange;
+  CharSourceRange ContentRange;
   bool HasSingleEntry;
   bool ThrowingUnhandledError;
   OrphanKind Orphan;
@@ -283,13 +285,16 @@
   Expr* CommonExprParent;
 
   ResolvedRangeInfo(RangeKind Kind, ReturnInfo ExitInfo,
-                    CharSourceRange Content, DeclContext* RangeContext,
+                    ArrayRef<Token> TokensInRange,
+                    DeclContext* RangeContext,
                     Expr *CommonExprParent, bool HasSingleEntry,
                     bool ThrowingUnhandledError,
                     OrphanKind Orphan, ArrayRef<ASTNode> ContainedNodes,
                     ArrayRef<DeclaredDecl> DeclaredDecls,
                     ArrayRef<ReferencedDecl> ReferencedDecls): Kind(Kind),
-                      ExitInfo(ExitInfo), Content(Content),
+                      ExitInfo(ExitInfo),
+                      TokensInRange(TokensInRange),
+                      ContentRange(calculateContentRange(TokensInRange)),
                       HasSingleEntry(HasSingleEntry),
                       ThrowingUnhandledError(ThrowingUnhandledError),
                       Orphan(Orphan), ContainedNodes(ContainedNodes),
@@ -297,15 +302,17 @@
                       ReferencedDecls(ReferencedDecls),
                       RangeContext(RangeContext),
                       CommonExprParent(CommonExprParent) {}
-  ResolvedRangeInfo(CharSourceRange Content) :
-  ResolvedRangeInfo(RangeKind::Invalid, {nullptr, ExitState::Unsure}, Content,
-                    nullptr, /*Commom Expr Parent*/nullptr,
+  ResolvedRangeInfo(ArrayRef<Token> TokensInRange) :
+  ResolvedRangeInfo(RangeKind::Invalid, {nullptr, ExitState::Unsure},
+                    TokensInRange, nullptr, /*Commom Expr Parent*/nullptr,
                     /*Single entry*/true, /*unhandled error*/false,
                     OrphanKind::None, {}, {}, {}) {}
-  ResolvedRangeInfo(): ResolvedRangeInfo(CharSourceRange()) {}
   void print(llvm::raw_ostream &OS);
   ExitState exit() const { return ExitInfo.Exit; }
   Type getType() const { return ExitInfo.ReturnType; }
+
+private:
+  static CharSourceRange calculateContentRange(ArrayRef<Token> Tokens);
 };
 
 class RangeResolver : public SourceEntityWalker {
diff --git a/lib/IDE/SwiftSourceDocInfo.cpp b/lib/IDE/SwiftSourceDocInfo.cpp
index b4af1ff..affcfc1 100644
--- a/lib/IDE/SwiftSourceDocInfo.cpp
+++ b/lib/IDE/SwiftSourceDocInfo.cpp
@@ -242,7 +242,7 @@
   }
   OS << "</Kind>\n";
 
-  OS << "<Content>" << Content.str() << "</Content>\n";
+  OS << "<Content>" << ContentRange.str() << "</Content>\n";
 
   if (auto Ty = getType()) {
     OS << "<Type>";
@@ -311,6 +311,19 @@
   OS << "<end>\n";
 }
 
+CharSourceRange ResolvedRangeInfo::
+calculateContentRange(ArrayRef<Token> Tokens) {
+  if (Tokens.empty())
+    return CharSourceRange();
+  auto StartTok = Tokens.front();
+  auto EndTok = Tokens.back();
+  auto StartLoc = StartTok.hasComment() ?
+    StartTok.getCommentStart() : StartTok.getLoc();
+  auto EndLoc = EndTok.getRange().getEnd();
+  return CharSourceRange(StartLoc, (char*)EndLoc.getOpaquePointerValue() -
+    (char*)StartLoc.getOpaquePointerValue());
+}
+
 bool DeclaredDecl::operator==(const DeclaredDecl& Other) {
   return VD == Other.VD;
 }
@@ -408,12 +421,13 @@
     }
   };
 
-  std::vector<Token> AllTokens;
-  Token &StartTok;
-  Token &EndTok;
+
+  std::vector<Token> TokensInRange;
+  const Token &StartTok;
+  const Token &EndTok;
   SourceLoc Start;
   SourceLoc End;
-  CharSourceRange Content;
+
   Optional<ResolvedRangeInfo> Result;
   std::vector<ContextInfo> ContextStack;
   ContextInfo &getCurrentDC() {
@@ -496,7 +510,7 @@
     if (Node.is<Expr*>())
       return ResolvedRangeInfo(RangeKind::SingleExpression,
                                resolveNodeType(Node, RangeKind::SingleExpression),
-                               Content,
+                               TokensInRange,
                                getImmediateContext(),
                                /*Common Parent Expr*/nullptr,
                                SingleEntry,
@@ -507,7 +521,8 @@
     else if (Node.is<Stmt*>())
       return ResolvedRangeInfo(RangeKind::SingleStatement,
                                resolveNodeType(Node, RangeKind::SingleStatement),
-                               Content, getImmediateContext(),
+                               TokensInRange,
+                               getImmediateContext(),
                                /*Common Parent Expr*/nullptr,
                                SingleEntry,
                                UnhandledError, Kind,
@@ -517,7 +532,8 @@
     else {
       assert(Node.is<Decl*>());
       return ResolvedRangeInfo(RangeKind::SingleDecl,
-                               ReturnInfo(), Content,
+                               ReturnInfo(),
+                               TokensInRange,
                                getImmediateContext(),
                                /*Common Parent Expr*/nullptr,
                                SingleEntry,
@@ -544,11 +560,13 @@
     return static_cast<DeclContext*>(&File);
   }
 
-  Implementation(SourceFile &File, std::vector<Token> AllTokens,
-                 unsigned StartIdx, unsigned EndIdx) :
+  Implementation(SourceFile &File, ArrayRef<Token> TokensInRange) :
     File(File), Ctx(File.getASTContext()), SM(Ctx.SourceMgr),
-    AllTokens(AllTokens), StartTok(AllTokens[StartIdx]), EndTok(AllTokens[EndIdx]),
-    Start(StartTok.getLoc()), End(EndTok.getLoc()), Content(getContentRange()) {
+    TokensInRange(TokensInRange),
+    StartTok(TokensInRange.front()),
+    EndTok(TokensInRange.back()),
+    Start(StartTok.getLoc()),
+    End(EndTok.getLoc()) {
       assert(Start.isValid() && End.isValid());
   }
 
@@ -576,7 +594,9 @@
     if (!hasResult() && !Node.isImplicit() && nodeContainSelection(Node)) {
       if (auto Parent = Node.is<Expr*>() ? Node.get<Expr*>() : nullptr) {
         Result = {
-          RangeKind::PartOfExpression, ReturnInfo(), Content,
+          RangeKind::PartOfExpression,
+          ReturnInfo(),
+          TokensInRange,
           getImmediateContext(),
           Parent,
           hasSingleEntryPoint(ContainedASTNodes),
@@ -621,10 +641,8 @@
     // The start token is inclusive.
     unsigned StartIdx = StartIt - AllTokens.begin();
 
-    // The end token is exclusive.
-    unsigned EndIdx = EndIt - 1 - AllTokens.begin();
     return std::unique_ptr<Implementation>(new Implementation(File,
-      std::move(AllTokens), StartIdx, EndIdx));
+      llvm::makeArrayRef(AllTokens.data() + StartIdx, EndIt - StartIt)));
   }
 
   static std::unique_ptr<Implementation>
@@ -795,7 +813,8 @@
       Result = {RangeKind::MultiStatement,
                 /* Last node has the type */
                 resolveNodeType(DCInfo.EndMatches.back(),
-                                RangeKind::MultiStatement), Content,
+                                RangeKind::MultiStatement),
+                TokensInRange,
                 getImmediateContext(), nullptr,
                 hasSingleEntryPoint(ContainedASTNodes),
                 hasUnhandledError(ContainedASTNodes),
@@ -842,7 +861,7 @@
   ResolvedRangeInfo getResult() {
     if (Result.hasValue())
       return Result.getValue();
-    return ResolvedRangeInfo(Content);
+    return ResolvedRangeInfo(TokensInRange);
   }
 
   void analyzeDeclRef(ValueDecl *VD, SourceLoc Start, Type Ty,
@@ -895,13 +914,6 @@
     else
       return RangeMatchKind::NoneMatch;
   }
-
-  CharSourceRange getContentRange() {
-    SourceManager &SM = File.getASTContext().SourceMgr;
-    return CharSourceRange(SM, StartTok.hasComment() ?
-                            StartTok.getCommentStart() : StartTok.getLoc(),
-                           Lexer::getLocForEndOfToken(SM, End));
-  }
 };
 
 RangeResolver::RangeResolver(SourceFile &File, SourceLoc Start, SourceLoc End) :
@@ -963,7 +975,7 @@
 
 ResolvedRangeInfo RangeResolver::resolve() {
   if (!Impl)
-    return ResolvedRangeInfo(CharSourceRange());
+    return ResolvedRangeInfo({});
   Impl->enter(ASTNode());
   walk(Impl->File);
   return Impl->getResult();
diff --git a/test/Interpreter/SDK/libc.swift b/test/Interpreter/SDK/libc.swift
index f6f2c16..13a0e17 100644
--- a/test/Interpreter/SDK/libc.swift
+++ b/test/Interpreter/SDK/libc.swift
@@ -6,6 +6,9 @@
 
 // REQUIRES: executable_test
 
+// TODO: rdar://problem/33388782
+// REQUIRES: CPU=x86_64
+
 #if os(OSX) || os(iOS) || os(watchOS) || os(tvOS)
   import Darwin
 #elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android)
diff --git a/tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp b/tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp
index 8758d7f..c3e8840 100644
--- a/tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp
+++ b/tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp
@@ -1355,7 +1355,7 @@
       ASTInvok->applyTo(CompInvok);
       RangeInfo Result;
       Result.RangeKind = Lang.getUIDForRangeKind(Info.Kind);
-      Result.RangeContent = Info.Content.str();
+      Result.RangeContent = Info.ContentRange.str();
       switch (Info.Kind) {
       case RangeKind::SingleExpression: {
         SmallString<64> SS;