]> granicus.if.org Git - clang/commitdiff
When given unsaved files in clang_createTranslationUnitFromSourceFile,
authorDouglas Gregor <dgregor@apple.com>
Sat, 27 Feb 2010 01:32:48 +0000 (01:32 +0000)
committerDouglas Gregor <dgregor@apple.com>
Sat, 27 Feb 2010 01:32:48 +0000 (01:32 +0000)
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
lib/Frontend/ASTUnit.cpp
lib/Frontend/InitPreprocessor.cpp
tools/CIndex/CIndex.cpp

index 6d1a02fb4f73e458d3be08f12140e76539683ce1..c1238e5d88e7c445463dc619dbb7a15fbec7d44a 100644 (file)
@@ -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;
 };
index 8874622a24daaf36698b29c3c0c7cc1bd1e54674..ef14df10345ecafec0bcc1f59d21931ad29e78ba 100644 (file)
@@ -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;
     }
     
index b7ab3d8cd4523670e2aff0ba6f60baf44ea56583..8bcd3a83c00e65a62a817d06bc1c1650dac69cc2 100644 (file)
@@ -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;
     }
     
index df89d73ba8c0e64954c68047ac274dc3fc9e0080..e13dddfcad580592d80e99d52c482413e323dc1a 100644 (file)
@@ -966,7 +966,7 @@ clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
   llvm::SmallVector<ASTUnit::RemappedFile, 4> 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,