[A fix related to closing C++ header file descriptors on windows]

jean-yves desbree via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 26 07:10:20 PST 2015


I use clang 3.6.2 with qt creator 3.5.1 on windows 7 for parsing code in
this IDE.
It works well.

However, I can see that clang keeps a few times some file descriptors
opened on c++ header files (h files) after having parsed a cpp file (that
includes these h files).
The effect is that we cannot save these h files, what is quite frustrating
when using an IDE.

After debugging clang, I remarked that there was a file descriptor leak in
the method Preprocessor::HandleIncludeDirective
(file tools/clang/lib/Lex/PPDirectives.cpp)

The object 'FileEntry *File' is created (for a given h file) and after some
checks, the regular treatment calls EnterSourceFile.
The File object is detroyed during this call (quite deeply in the stack)

However, when some errors occur, the execution path goes through early
returns and other code pathes.
In this case, the file descriptor associated to File is not closed and the
file descriptor remains open.

So I did a correction that uses RAII in order to have the file descriptor
closed.
So I wrapped 'FileEntry *File' in a dedicated 'FileEntryCloser
fileEntryCloser(File)'

On regular treatment, the closer is released:
  // If all is good, enter the new file!
  if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation()))
  {
     fileEntryCloser.release();
     return;
  }

Otherwise, the file descriptor is closed
   ~FileEntryCloser()
   {
      if(m_File)
         m_File->closeFile();
   }


Now, I have no more remaining file descriptors ...
Would it be possible to have an evaluation on that?

Thanks in advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20151126/7bd94258/attachment.html>


More information about the cfe-commits mailing list