From c8dfe5ece04e683106eb96c58a2999f70b53ac21 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Sat, 27 Feb 2010 01:32:48 +0000 Subject: [PATCH] When given unsaved files in clang_createTranslationUnitFromSourceFile, copy the source buffers provided rather than referencing them directly, so that the caller can free those buffers immediately after calling clang_createTranslationUnitFromSourceFile(). Otherwise, we risk hitting those buffers later (when building source ranges, forming diagnostics, etc.). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@97296 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang-c/Index.h | 6 ++---- lib/Frontend/ASTUnit.cpp | 1 + lib/Frontend/InitPreprocessor.cpp | 3 ++- tools/CIndex/CIndex.cpp | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index 6d1a02fb4f..c1238e5d88 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -87,14 +87,12 @@ struct CXUnsavedFile { const char *Filename; /** - * \brief A null-terminated buffer containing the unsaved contents - * of this file. + * \brief A buffer containing the unsaved contents of this file. */ const char *Contents; /** - * \brief The length of the unsaved contents of this buffer, not - * counting the NULL at the end of the buffer. + * \brief The length of the unsaved contents of this buffer. */ unsigned long Length; }; diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp index 8874622a24..ef14df1034 100644 --- a/lib/Frontend/ASTUnit.cpp +++ b/lib/Frontend/ASTUnit.cpp @@ -162,6 +162,7 @@ ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, if (!FromFile) { Diags.Report(diag::err_fe_remap_missing_from_file) << RemappedFiles[I].first; + delete RemappedFiles[I].second; continue; } diff --git a/lib/Frontend/InitPreprocessor.cpp b/lib/Frontend/InitPreprocessor.cpp index b7ab3d8cd4..8bcd3a83c0 100644 --- a/lib/Frontend/InitPreprocessor.cpp +++ b/lib/Frontend/InitPreprocessor.cpp @@ -439,6 +439,7 @@ static void InitializeFileRemapping(Diagnostic &Diags, if (!FromFile) { Diags.Report(diag::err_fe_remap_missing_from_file) << Remap->first; + delete Remap->second; continue; } @@ -477,7 +478,7 @@ static void InitializeFileRemapping(Diagnostic &Diags, = llvm::MemoryBuffer::getFile(ToFile->getName(), &ErrorStr); if (!Buffer) { Diags.Report(diag::err_fe_error_opening) - << Remap->second << ErrorStr; + << Remap->second << ErrorStr; continue; } diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp index df89d73ba8..e13dddfcad 100644 --- a/tools/CIndex/CIndex.cpp +++ b/tools/CIndex/CIndex.cpp @@ -966,7 +966,7 @@ clang_createTranslationUnitFromSourceFile(CXIndex CIdx, llvm::SmallVector RemappedFiles; for (unsigned I = 0; I != num_unsaved_files; ++I) { const llvm::MemoryBuffer *Buffer - = llvm::MemoryBuffer::getMemBuffer(unsaved_files[I].Contents, + = llvm::MemoryBuffer::getMemBufferCopy(unsaved_files[I].Contents, unsaved_files[I].Contents + unsaved_files[I].Length, unsaved_files[I].Filename); RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, -- 2.40.0