@@ -3909,7 +3909,7 @@ Preprocessor::LexEmbedParameters(Token &CurTok, bool ForHasEmbed) {
39093909
39103910void Preprocessor::HandleEmbedDirectiveImpl (
39113911 SourceLocation HashLoc, const LexEmbedParametersResult &Params,
3912- StringRef BinaryContents) {
3912+ StringRef BinaryContents, StringRef FileName ) {
39133913 if (BinaryContents.empty ()) {
39143914 // If we have no binary contents, the only thing we need to emit are the
39153915 // if_empty tokens, if any.
@@ -3940,6 +3940,7 @@ void Preprocessor::HandleEmbedDirectiveImpl(
39403940
39413941 EmbedAnnotationData *Data = new (BP) EmbedAnnotationData;
39423942 Data->BinaryData = BinaryContents;
3943+ Data->FileName = FileName;
39433944
39443945 Toks[CurIdx].startToken ();
39453946 Toks[CurIdx].setKind (tok::annot_embed);
@@ -4049,5 +4050,16 @@ void Preprocessor::HandleEmbedDirective(SourceLocation HashLoc, Token &EmbedTok,
40494050 if (Callbacks)
40504051 Callbacks->EmbedDirective (HashLoc, Filename, isAngled, MaybeFileRef,
40514052 *Params);
4052- HandleEmbedDirectiveImpl (HashLoc, *Params, BinaryContents);
4053+ // getSpelling() may return a buffer from the token itself or it may use the
4054+ // SmallString buffer we provided. getSpelling() may also return a string that
4055+ // is actually longer than FilenameTok.getLength(), so we first pass a
4056+ // locally created buffer to getSpelling() to get the string of real length
4057+ // and then we allocate a long living buffer because the buffer we used
4058+ // previously will only live till the end of this function and we need
4059+ // filename info to live longer.
4060+ void *Mem = BP.Allocate (OriginalFilename.size (), alignof (char *));
4061+ memcpy (Mem, OriginalFilename.data (), OriginalFilename.size ());
4062+ StringRef FilenameToGo =
4063+ StringRef (static_cast <char *>(Mem), OriginalFilename.size ());
4064+ HandleEmbedDirectiveImpl (HashLoc, *Params, BinaryContents, FilenameToGo);
40534065}
0 commit comments