<div dir="ltr">Hey ho,<div><br></div><div>this looks like a really cool new feature, and the CL looks good to me; I was wondering whether in the future we can send CLs like this through pre-commit review, or have an RFC email thread. If you have more stuff landing, a single RFC email thread describing how the features work together would also be super useful, as it makes it easier to follow along the patches later.</div><div><br></div><div>Cheers,</div><div>/Manuel</div></div><br><div class="gmail_quote"><div dir="ltr">On Fri, Jun 9, 2017 at 3:21 AM Argyrios Kyrtzidis via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: akirtzidis<br>
Date: Thu Jun  8 20:20:48 2017<br>
New Revision: 305044<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=305044&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=305044&view=rev</a><br>
Log:<br>
[libclang] Introduce a new parsing option 'CXTranslationUnit_SingleFileParse' that puts preprocessor in a mode for parsing a single file only.<br>
<br>
This is useful for parsing a single file, as a fast/inaccurate 'mode' that can still provide declarations from the file, like the classes and their methods.<br>
<br>
Added:<br>
    cfe/trunk/test/Index/singe-file-parse.m<br>
Modified:<br>
    cfe/trunk/include/clang-c/Index.h<br>
    cfe/trunk/include/clang/Frontend/ASTUnit.h<br>
    cfe/trunk/include/clang/Lex/PreprocessorOptions.h<br>
    cfe/trunk/lib/Frontend/ASTUnit.cpp<br>
    cfe/trunk/lib/Lex/PPDirectives.cpp<br>
    cfe/trunk/tools/c-index-test/c-index-test.c<br>
    cfe/trunk/tools/libclang/CIndex.cpp<br>
<br>
Modified: cfe/trunk/include/clang-c/Index.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=305044&r1=305043&r2=305044&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=305044&r1=305043&r2=305044&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/include/clang-c/Index.h (original)<br>
+++ cfe/trunk/include/clang-c/Index.h Thu Jun  8 20:20:48 2017<br>
@@ -32,7 +32,7 @@<br>
  * compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable.<br>
  */<br>
 #define CINDEX_VERSION_MAJOR 0<br>
-#define CINDEX_VERSION_MINOR 42<br>
+#define CINDEX_VERSION_MINOR 43<br>
<br>
 #define CINDEX_VERSION_ENCODE(major, minor) ( \<br>
       ((major) * 10000)                       \<br>
@@ -1234,7 +1234,12 @@ enum CXTranslationUnit_Flags {<br>
    * purposes of an IDE, this is undesirable behavior and as much information<br>
    * as possible should be reported. Use this flag to enable this behavior.<br>
    */<br>
-  CXTranslationUnit_KeepGoing = 0x200<br>
+  CXTranslationUnit_KeepGoing = 0x200,<br>
+<br>
+  /**<br>
+   * \brief Sets the preprocessor in a mode for parsing a single file only.<br>
+   */<br>
+  CXTranslationUnit_SingleFileParse = 0x400<br>
 };<br>
<br>
 /**<br>
<br>
Modified: cfe/trunk/include/clang/Frontend/ASTUnit.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/ASTUnit.h?rev=305044&r1=305043&r2=305044&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/ASTUnit.h?rev=305044&r1=305043&r2=305044&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/include/clang/Frontend/ASTUnit.h (original)<br>
+++ cfe/trunk/include/clang/Frontend/ASTUnit.h Thu Jun  8 20:20:48 2017<br>
@@ -871,6 +871,7 @@ public:<br>
       bool CacheCodeCompletionResults = false,<br>
       bool IncludeBriefCommentsInCodeCompletion = false,<br>
       bool AllowPCHWithCompilerErrors = false, bool SkipFunctionBodies = false,<br>
+      bool SingleFileParse = false,<br>
       bool UserFilesAreVolatile = false, bool ForSerialization = false,<br>
       llvm::Optional<StringRef> ModuleFormat = llvm::None,<br>
       std::unique_ptr<ASTUnit> *ErrAST = nullptr,<br>
<br>
Modified: cfe/trunk/include/clang/Lex/PreprocessorOptions.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/PreprocessorOptions.h?rev=305044&r1=305043&r2=305044&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/PreprocessorOptions.h?rev=305044&r1=305043&r2=305044&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/include/clang/Lex/PreprocessorOptions.h (original)<br>
+++ cfe/trunk/include/clang/Lex/PreprocessorOptions.h Thu Jun  8 20:20:48 2017<br>
@@ -95,6 +95,9 @@ public:<br>
   /// If given, a PTH cache file to use for speeding up header parsing.<br>
   std::string TokenCache;<br>
<br>
+  /// When enabled, preprocessor is in a mode for parsing a single file only.<br>
+  bool SingleFileParseMode = false;<br>
+<br>
   /// \brief True if the SourceManager should report the original file name for<br>
   /// contents of files that were remapped to other files. Defaults to true.<br>
   bool RemappedFilesKeepOriginalName;<br>
@@ -181,6 +184,7 @@ public:<br>
     ImplicitPCHInclude.clear();<br>
     ImplicitPTHInclude.clear();<br>
     TokenCache.clear();<br>
+    SingleFileParseMode = false;<br>
     RetainRemappedFileBuffers = true;<br>
     PrecompiledPreambleBytes.first = 0;<br>
     PrecompiledPreambleBytes.second = 0;<br>
<br>
Modified: cfe/trunk/lib/Frontend/ASTUnit.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ASTUnit.cpp?rev=305044&r1=305043&r2=305044&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ASTUnit.cpp?rev=305044&r1=305043&r2=305044&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Frontend/ASTUnit.cpp (original)<br>
+++ cfe/trunk/lib/Frontend/ASTUnit.cpp Thu Jun  8 20:20:48 2017<br>
@@ -1982,7 +1982,7 @@ ASTUnit *ASTUnit::LoadFromCommandLine(<br>
     unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,<br>
     bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,<br>
     bool AllowPCHWithCompilerErrors, bool SkipFunctionBodies,<br>
-    bool UserFilesAreVolatile, bool ForSerialization,<br>
+    bool SingleFileParse, bool UserFilesAreVolatile, bool ForSerialization,<br>
     llvm::Optional<StringRef> ModuleFormat, std::unique_ptr<ASTUnit> *ErrAST,<br>
     IntrusiveRefCntPtr<vfs::FileSystem> VFS) {<br>
   assert(Diags.get() && "no DiagnosticsEngine was provided");<br>
@@ -2011,6 +2011,7 @@ ASTUnit *ASTUnit::LoadFromCommandLine(<br>
   PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName;<br>
   PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors;<br>
   PPOpts.GeneratePreamble = PrecompilePreambleAfterNParses != 0;<br>
+  PPOpts.SingleFileParseMode = SingleFileParse;<br>
<br>
   // Override the resources path.<br>
   CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;<br>
<br>
Modified: cfe/trunk/lib/Lex/PPDirectives.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPDirectives.cpp?rev=305044&r1=305043&r2=305044&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPDirectives.cpp?rev=305044&r1=305043&r2=305044&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Lex/PPDirectives.cpp (original)<br>
+++ cfe/trunk/lib/Lex/PPDirectives.cpp Thu Jun  8 20:20:48 2017<br>
@@ -30,6 +30,7 @@<br>
 #include "clang/Lex/PPCallbacks.h"<br>
 #include "clang/Lex/Pragma.h"<br>
 #include "clang/Lex/Preprocessor.h"<br>
+#include "clang/Lex/PreprocessorOptions.h"<br>
 #include "clang/Lex/PTHLexer.h"<br>
 #include "clang/Lex/Token.h"<br>
 #include "llvm/ADT/ArrayRef.h"<br>
@@ -1845,10 +1846,13 @@ void Preprocessor::HandleIncludeDirectiv<br>
   // we've imported or already built.<br>
   bool ShouldEnter = true;<br>
<br>
+  if (PPOpts->SingleFileParseMode)<br>
+    ShouldEnter = false;<br>
+<br>
   // Determine whether we should try to import the module for this #include, if<br>
   // there is one. Don't do so if precompiled module support is disabled or we<br>
   // are processing this module textually (because we're building the module).<br>
-  if (File && SuggestedModule && getLangOpts().Modules &&<br>
+  if (ShouldEnter && File && SuggestedModule && getLangOpts().Modules &&<br>
       SuggestedModule.getModule()->getTopLevelModuleName() !=<br>
           getLangOpts().CurrentModule) {<br>
     // If this include corresponds to a module but that module is<br>
<br>
Added: cfe/trunk/test/Index/singe-file-parse.m<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/singe-file-parse.m?rev=305044&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/singe-file-parse.m?rev=305044&view=auto</a><br>
==============================================================================<br>
--- cfe/trunk/test/Index/singe-file-parse.m (added)<br>
+++ cfe/trunk/test/Index/singe-file-parse.m Thu Jun  8 20:20:48 2017<br>
@@ -0,0 +1,11 @@<br>
+// RUN: c-index-test -single-file-parse %s | FileCheck %s<br>
+<br>
+#include <stdint.h><br>
+<br>
+// CHECK-NOT: TypedefDecl=intptr_t<br>
+<br>
+// CHECK: [[@LINE+1]]:12: ObjCInterfaceDecl=MyCls<br>
+@interface MyCls<br>
+// CHECK: [[@LINE+1]]:8: ObjCInstanceMethodDecl=some_meth<br>
+-(void)some_meth;<br>
+@end<br>
<br>
Modified: cfe/trunk/tools/c-index-test/c-index-test.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=305044&r1=305043&r2=305044&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=305044&r1=305043&r2=305044&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)<br>
+++ cfe/trunk/tools/c-index-test/c-index-test.c Thu Jun  8 20:20:48 2017<br>
@@ -1850,6 +1850,34 @@ int perform_test_reparse_source(int argc<br>
   return result;<br>
 }<br>
<br>
+static int perform_single_file_parse(const char *filename) {<br>
+  CXIndex Idx;<br>
+  CXTranslationUnit TU;<br>
+  enum CXErrorCode Err;<br>
+  int result;<br>
+<br>
+  Idx = clang_createIndex(/* excludeDeclsFromPCH */1,<br>
+                          /* displayDiagnostics=*/1);<br>
+<br>
+  Err = clang_parseTranslationUnit2(Idx, filename,<br>
+                                    /*command_line_args=*/NULL,<br>
+                                    /*num_command_line_args=*/0,<br>
+                                    /*unsaved_files=*/NULL,<br>
+                                    /*num_unsaved_files=*/0,<br>
+                                    CXTranslationUnit_SingleFileParse, &TU);<br>
+  if (Err != CXError_Success) {<br>
+    fprintf(stderr, "Unable to load translation unit!\n");<br>
+    describeLibclangFailure(Err);<br>
+    clang_disposeIndex(Idx);<br>
+    return 1;<br>
+  }<br>
+<br>
+  result = perform_test_load(Idx, TU, /*filter=*/"all", /*prefix=*/NULL, FilteredPrintingVisitor, /*PostVisit=*/NULL,<br>
+                             /*CommentSchemaFile=*/NULL);<br>
+  clang_disposeIndex(Idx);<br>
+  return result;<br>
+}<br>
+<br>
 /******************************************************************************/<br>
 /* Logic for testing clang_getCursor().                                       */<br>
 /******************************************************************************/<br>
@@ -4439,6 +4467,8 @@ int cindextest_main(int argc, const char<br>
       return perform_test_load_source(argc - 3, argv + 3, argv[2], I,<br>
                                       postVisit);<br>
   }<br>
+  else if (argc >= 3 && strcmp(argv[1], "-single-file-parse") == 0)<br>
+    return perform_single_file_parse(argv[2]);<br>
   else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)<br>
     return perform_file_scan(argv[2], argv[3],<br>
                              argc >= 5 ? argv[4] : 0);<br>
<br>
Modified: cfe/trunk/tools/libclang/CIndex.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=305044&r1=305043&r2=305044&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=305044&r1=305043&r2=305044&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/tools/libclang/CIndex.cpp (original)<br>
+++ cfe/trunk/tools/libclang/CIndex.cpp Thu Jun  8 20:20:48 2017<br>
@@ -3300,12 +3300,14 @@ clang_parseTranslationUnit_Impl(CXIndex<br>
       options & CXTranslationUnit_CreatePreambleOnFirstParse;<br>
   // FIXME: Add a flag for modules.<br>
   TranslationUnitKind TUKind<br>
-    = (options & CXTranslationUnit_Incomplete)? TU_Prefix : TU_Complete;<br>
+    = (options & (CXTranslationUnit_Incomplete |<br>
+                  CXTranslationUnit_SingleFileParse))? TU_Prefix : TU_Complete;<br>
   bool CacheCodeCompletionResults<br>
     = options & CXTranslationUnit_CacheCompletionResults;<br>
   bool IncludeBriefCommentsInCodeCompletion<br>
     = options & CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;<br>
   bool SkipFunctionBodies = options & CXTranslationUnit_SkipFunctionBodies;<br>
+  bool SingleFileParse = options & CXTranslationUnit_SingleFileParse;<br>
   bool ForSerialization = options & CXTranslationUnit_ForSerialization;<br>
<br>
   // Configure the diagnostics.<br>
@@ -3390,7 +3392,7 @@ clang_parseTranslationUnit_Impl(CXIndex<br>
       /*CaptureDiagnostics=*/true, *RemappedFiles.get(),<br>
       /*RemappedFilesKeepOriginalName=*/true, PrecompilePreambleAfterNParses,<br>
       TUKind, CacheCodeCompletionResults, IncludeBriefCommentsInCodeCompletion,<br>
-      /*AllowPCHWithCompilerErrors=*/true, SkipFunctionBodies,<br>
+      /*AllowPCHWithCompilerErrors=*/true, SkipFunctionBodies, SingleFileParse,<br>
       /*UserFilesAreVolatile=*/true, ForSerialization,<br>
       CXXIdx->getPCHContainerOperations()->getRawReader().getFormat(),<br>
       &ErrUnit));<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br>
</blockquote></div>