juliehockett updated this revision to Diff 145934.
juliehockett added a comment.

Adding test


https://reviews.llvm.org/D46614

Files:
  include/clang/Lex/PPCallbacks.h
  include/clang/Lex/PreprocessingRecord.h
  lib/CodeGen/MacroPPCallbacks.cpp
  lib/CodeGen/MacroPPCallbacks.h
  lib/Frontend/DependencyFile.cpp
  lib/Frontend/DependencyGraph.cpp
  lib/Frontend/ModuleDependencyCollector.cpp
  lib/Frontend/PrintPreprocessedOutput.cpp
  lib/Frontend/Rewrite/InclusionRewriter.cpp
  lib/Lex/PPDirectives.cpp
  lib/Lex/PreprocessingRecord.cpp
  tools/libclang/Indexing.cpp
  unittests/Lex/PPCallbacksTest.cpp

Index: unittests/Lex/PPCallbacksTest.cpp
===================================================================
--- unittests/Lex/PPCallbacksTest.cpp
+++ unittests/Lex/PPCallbacksTest.cpp
@@ -39,16 +39,18 @@
                           StringRef FileName, bool IsAngled,
                           CharSourceRange FilenameRange, const FileEntry *File,
                           StringRef SearchPath, StringRef RelativePath,
-                          const Module *Imported) override {
-      this->HashLoc = HashLoc;
-      this->IncludeTok = IncludeTok;
-      this->FileName = FileName.str();
-      this->IsAngled = IsAngled;
-      this->FilenameRange = FilenameRange;
-      this->File = File;
-      this->SearchPath = SearchPath.str();
-      this->RelativePath = RelativePath.str();
-      this->Imported = Imported;
+                          const Module *Imported,
+                          SrcMgr::CharacteristicKind FileType) override {
+    this->HashLoc = HashLoc;
+    this->IncludeTok = IncludeTok;
+    this->FileName = FileName.str();
+    this->IsAngled = IsAngled;
+    this->FilenameRange = FilenameRange;
+    this->File = File;
+    this->SearchPath = SearchPath.str();
+    this->RelativePath = RelativePath.str();
+    this->Imported = Imported;
+    this->FileType = FileType;
   }
 
   SourceLocation HashLoc;
@@ -60,6 +62,7 @@
   SmallString<16> SearchPath;
   SmallString<16> RelativePath;
   const Module* Imported;
+  SrcMgr::CharacteristicKind FileType;
 };
 
 // Stub to collect data from PragmaOpenCLExtension callbacks.
@@ -136,8 +139,9 @@
 
   // Run lexer over SourceText and collect FilenameRange from
   // the InclusionDirective callback.
-  CharSourceRange InclusionDirectiveFilenameRange(const char* SourceText, 
-      const char* HeaderPath, bool SystemHeader) {
+  InclusionDirectiveCallbacks *
+  InclusionDirectiveCallback(const char *SourceText,
+                                  const char *HeaderPath, bool SystemHeader) {
     std::unique_ptr<llvm::MemoryBuffer> Buf =
         llvm::MemoryBuffer::getMemBuffer(SourceText);
     SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
@@ -168,7 +172,7 @@
     }
 
     // Callbacks have been executed at this point -- return filename range.
-    return Callbacks->FilenameRange;
+    return Callbacks;
   }
 
   PragmaOpenCLExtensionCallbacks::CallbackParameters 
@@ -222,12 +226,21 @@
   }
 };
 
+TEST_F(PPCallbacksTest, UserFileCharacteristics) {
+  const char *Source = "#include \"quoted.h\"\n";
+
+  SrcMgr::CharacteristicKind Kind =
+      InclusionDirectiveCallback(Source, "/quoted.h", false)->FileType;
+
+  ASSERT_EQ(SrcMgr::CharacteristicKind::C_User, Kind);
+}
+
 TEST_F(PPCallbacksTest, QuotedFilename) {
   const char* Source =
     "#include \"quoted.h\"\n";
 
   CharSourceRange Range =
-    InclusionDirectiveFilenameRange(Source, "/quoted.h", false);
+    InclusionDirectiveCallback(Source, "/quoted.h", false)->FilenameRange;
 
   ASSERT_EQ("\"quoted.h\"", GetSourceString(Range));
 }
@@ -237,7 +250,7 @@
     "#include <angled.h>\n";
 
   CharSourceRange Range =
-    InclusionDirectiveFilenameRange(Source, "/angled.h", true);
+    InclusionDirectiveCallback(Source, "/angled.h", true)->FilenameRange;
 
   ASSERT_EQ("<angled.h>", GetSourceString(Range));
 }
@@ -248,7 +261,7 @@
     "#include MACRO_QUOTED\n";
 
   CharSourceRange Range =
-    InclusionDirectiveFilenameRange(Source, "/quoted.h", false);
+    InclusionDirectiveCallback(Source, "/quoted.h", false)->FilenameRange;
 
   ASSERT_EQ("\"quoted.h\"", GetSourceString(Range));
 }
@@ -259,7 +272,7 @@
     "#include MACRO_ANGLED\n";
 
   CharSourceRange Range =
-    InclusionDirectiveFilenameRange(Source, "/angled.h", true);
+    InclusionDirectiveCallback(Source, "/angled.h", true)->FilenameRange;
 
   ASSERT_EQ("<angled.h>", GetSourceString(Range));
 }
@@ -270,7 +283,7 @@
     "#include MACRO_STRINGIZED(quoted.h)\n";
 
   CharSourceRange Range =
-    InclusionDirectiveFilenameRange(Source, "/quoted.h", false);
+    InclusionDirectiveCallback(Source, "/quoted.h", false)->FilenameRange;
 
   ASSERT_EQ("\"quoted.h\"", GetSourceString(Range));
 }
@@ -282,7 +295,7 @@
     "#include MACRO_CONCAT(MACRO, ANGLED)\n";
 
   CharSourceRange Range =
-    InclusionDirectiveFilenameRange(Source, "/angled.h", false);
+    InclusionDirectiveCallback(Source, "/angled.h", false)->FilenameRange;
 
   ASSERT_EQ("<angled.h>", GetSourceString(Range));
 }
@@ -292,7 +305,7 @@
     "#include \"tri\?\?-graph.h\"\n";
 
   CharSourceRange Range =
-    InclusionDirectiveFilenameRange(Source, "/tri~graph.h", false);
+    InclusionDirectiveCallback(Source, "/tri~graph.h", false)->FilenameRange;
 
   ASSERT_EQ("\"tri\?\?-graph.h\"", GetSourceString(Range));
 }
@@ -303,7 +316,7 @@
     "#include MACRO_TRIGRAPH\n";
 
   CharSourceRange Range =
-    InclusionDirectiveFilenameRange(Source, "/tri~graph.h", false);
+    InclusionDirectiveCallback(Source, "/tri~graph.h", false)->FilenameRange;
 
   ASSERT_EQ("\"tri\?\?-graph.h\"", GetSourceString(Range));
 }
Index: tools/libclang/Indexing.cpp
===================================================================
--- tools/libclang/Indexing.cpp
+++ tools/libclang/Indexing.cpp
@@ -249,7 +249,8 @@
                           StringRef FileName, bool IsAngled,
                           CharSourceRange FilenameRange, const FileEntry *File,
                           StringRef SearchPath, StringRef RelativePath,
-                          const Module *Imported) override {
+                          const Module *Imported,
+                          SrcMgr::CharacteristicKind FileType) override {
     bool isImport = (IncludeTok.is(tok::identifier) &&
             IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import);
     DataConsumer.ppIncludedFile(HashLoc, FileName, File, isImport, IsAngled,
Index: lib/Lex/PreprocessingRecord.cpp
===================================================================
--- lib/Lex/PreprocessingRecord.cpp
+++ lib/Lex/PreprocessingRecord.cpp
@@ -471,7 +471,8 @@
     const FileEntry *File,
     StringRef SearchPath,
     StringRef RelativePath,
-    const Module *Imported) {
+    const Module *Imported, 
+    SrcMgr::CharacteristicKind FileType) {
   InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
   
   switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
Index: lib/Lex/PPDirectives.cpp
===================================================================
--- lib/Lex/PPDirectives.cpp
+++ lib/Lex/PPDirectives.cpp
@@ -1968,7 +1968,7 @@
         HashLoc, IncludeTok,
         LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
         FilenameRange, File, SearchPath, RelativePath,
-        ShouldEnter ? nullptr : SuggestedModule.getModule());
+        ShouldEnter ? nullptr : SuggestedModule.getModule(), FileCharacter);
     if (SkipHeader && !SuggestedModule.getModule())
       Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
   }
Index: lib/Frontend/Rewrite/InclusionRewriter.cpp
===================================================================
--- lib/Frontend/Rewrite/InclusionRewriter.cpp
+++ lib/Frontend/Rewrite/InclusionRewriter.cpp
@@ -77,7 +77,8 @@
                           StringRef FileName, bool IsAngled,
                           CharSourceRange FilenameRange, const FileEntry *File,
                           StringRef SearchPath, StringRef RelativePath,
-                          const Module *Imported) override;
+                          const Module *Imported,
+                          SrcMgr::CharacteristicKind FileType) override;
   void WriteLineInfo(StringRef Filename, int Line,
                      SrcMgr::CharacteristicKind FileType,
                      StringRef Extra = StringRef());
@@ -192,7 +193,8 @@
                                            const FileEntry * /*File*/,
                                            StringRef /*SearchPath*/,
                                            StringRef /*RelativePath*/,
-                                           const Module *Imported) {
+                                           const Module *Imported,
+                                           SrcMgr::CharacteristicKind FileType){
   if (Imported) {
     auto P = ModuleIncludes.insert(
         std::make_pair(HashLoc.getRawEncoding(), Imported));
Index: lib/Frontend/PrintPreprocessedOutput.cpp
===================================================================
--- lib/Frontend/PrintPreprocessedOutput.cpp
+++ lib/Frontend/PrintPreprocessedOutput.cpp
@@ -130,7 +130,8 @@
                           StringRef FileName, bool IsAngled,
                           CharSourceRange FilenameRange, const FileEntry *File,
                           StringRef SearchPath, StringRef RelativePath,
-                          const Module *Imported) override;
+                          const Module *Imported,
+                          SrcMgr::CharacteristicKind FileType) override;
   void Ident(SourceLocation Loc, StringRef str) override;
   void PragmaMessage(SourceLocation Loc, StringRef Namespace,
                      PragmaMessageKind Kind, StringRef Str) override;
@@ -320,15 +321,17 @@
   }
 }
 
-void PrintPPOutputPPCallbacks::InclusionDirective(SourceLocation HashLoc,
-                                                  const Token &IncludeTok,
-                                                  StringRef FileName,
-                                                  bool IsAngled,
-                                                  CharSourceRange FilenameRange,
-                                                  const FileEntry *File,
-                                                  StringRef SearchPath,
-                                                  StringRef RelativePath,
-                                                  const Module *Imported) {
+void PrintPPOutputPPCallbacks::InclusionDirective(
+    SourceLocation HashLoc,
+    const Token &IncludeTok,
+    StringRef FileName,
+    bool IsAngled,
+    CharSourceRange FilenameRange,
+    const FileEntry *File,
+    StringRef SearchPath,
+    StringRef RelativePath,
+    const Module *Imported,
+    SrcMgr::CharacteristicKind FileType) {
   // In -dI mode, dump #include directives prior to dumping their content or
   // interpretation.
   if (DumpIncludeDirectives) {
Index: lib/Frontend/ModuleDependencyCollector.cpp
===================================================================
--- lib/Frontend/ModuleDependencyCollector.cpp
+++ lib/Frontend/ModuleDependencyCollector.cpp
@@ -50,7 +50,8 @@
                           StringRef FileName, bool IsAngled,
                           CharSourceRange FilenameRange, const FileEntry *File,
                           StringRef SearchPath, StringRef RelativePath,
-                          const Module *Imported) override {
+                          const Module *Imported,
+                          SrcMgr::CharacteristicKind FileType) override {
     if (!File)
       return;
     Collector.addFile(File->getName());
Index: lib/Frontend/DependencyGraph.cpp
===================================================================
--- lib/Frontend/DependencyGraph.cpp
+++ lib/Frontend/DependencyGraph.cpp
@@ -50,7 +50,8 @@
                           StringRef FileName, bool IsAngled,
                           CharSourceRange FilenameRange, const FileEntry *File,
                           StringRef SearchPath, StringRef RelativePath,
-                          const Module *Imported) override;
+                          const Module *Imported,
+                          SrcMgr::CharacteristicKind FileType) override;
 
   void EndOfMainFile() override {
     OutputGraphFile();
@@ -65,15 +66,17 @@
                                                                SysRoot));
 }
 
-void DependencyGraphCallback::InclusionDirective(SourceLocation HashLoc,
-                                                 const Token &IncludeTok,
-                                                 StringRef FileName,
-                                                 bool IsAngled,
-                                                 CharSourceRange FilenameRange,
-                                                 const FileEntry *File,
-                                                 StringRef SearchPath,
-                                                 StringRef RelativePath,
-                                                 const Module *Imported) {
+void DependencyGraphCallback::InclusionDirective(
+    SourceLocation HashLoc,
+    const Token &IncludeTok,
+    StringRef FileName,
+    bool IsAngled,
+    CharSourceRange FilenameRange,
+    const FileEntry *File,
+    StringRef SearchPath,
+    StringRef RelativePath,
+    const Module *Imported, 
+    SrcMgr::CharacteristicKind FileType) {
   if (!File)
     return;
   
Index: lib/Frontend/DependencyFile.cpp
===================================================================
--- lib/Frontend/DependencyFile.cpp
+++ lib/Frontend/DependencyFile.cpp
@@ -63,7 +63,8 @@
                           StringRef FileName, bool IsAngled,
                           CharSourceRange FilenameRange, const FileEntry *File,
                           StringRef SearchPath, StringRef RelativePath,
-                          const Module *Imported) override {
+                          const Module *Imported,
+                          SrcMgr::CharacteristicKind FileType) override {
     if (!File)
       DepCollector.maybeAddDependency(FileName, /*FromModule*/false,
                                      /*IsSystem*/false, /*IsModuleFile*/false,
@@ -193,7 +194,8 @@
                           StringRef FileName, bool IsAngled,
                           CharSourceRange FilenameRange, const FileEntry *File,
                           StringRef SearchPath, StringRef RelativePath,
-                          const Module *Imported) override;
+                          const Module *Imported,
+                          SrcMgr::CharacteristicKind FileType) override;
 
   void EndOfMainFile() override {
     OutputDependencyFile();
@@ -313,7 +315,8 @@
                                  const FileEntry *File,
                                  StringRef SearchPath,
                                  StringRef RelativePath,
-                                 const Module *Imported) {
+                                 const Module *Imported, 
+                                 SrcMgr::CharacteristicKind FileType) {
   if (!File) {
     if (AddMissingHeaderDeps)
       AddFilename(FileName);
Index: lib/CodeGen/MacroPPCallbacks.h
===================================================================
--- lib/CodeGen/MacroPPCallbacks.h
+++ lib/CodeGen/MacroPPCallbacks.h
@@ -101,7 +101,8 @@
                           StringRef FileName, bool IsAngled,
                           CharSourceRange FilenameRange, const FileEntry *File,
                           StringRef SearchPath, StringRef RelativePath,
-                          const Module *Imported) override;
+                          const Module *Imported,
+                          SrcMgr::CharacteristicKind FileType) override;
 
   /// Hook called whenever a macro definition is seen.
   void MacroDefined(const Token &MacroNameTok,
Index: lib/CodeGen/MacroPPCallbacks.cpp
===================================================================
--- lib/CodeGen/MacroPPCallbacks.cpp
+++ lib/CodeGen/MacroPPCallbacks.cpp
@@ -178,7 +178,8 @@
 void MacroPPCallbacks::InclusionDirective(
     SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
     bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
-    StringRef SearchPath, StringRef RelativePath, const Module *Imported) {
+    StringRef SearchPath, StringRef RelativePath, const Module *Imported,
+    SrcMgr::CharacteristicKind FileType) {
 
   // Record the line location of the current included file.
   LastHashLoc = HashLoc;
Index: include/clang/Lex/PreprocessingRecord.h
===================================================================
--- include/clang/Lex/PreprocessingRecord.h
+++ include/clang/Lex/PreprocessingRecord.h
@@ -532,8 +532,8 @@
                             StringRef FileName, bool IsAngled,
                             CharSourceRange FilenameRange,
                             const FileEntry *File, StringRef SearchPath,
-                            StringRef RelativePath,
-                            const Module *Imported) override;
+                            StringRef RelativePath, const Module *Imported,
+                            SrcMgr::CharacteristicKind FileType) override;
     void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
                const MacroDefinition &MD) override;
     void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
Index: include/clang/Lex/PPCallbacks.h
===================================================================
--- include/clang/Lex/PPCallbacks.h
+++ include/clang/Lex/PPCallbacks.h
@@ -84,22 +84,22 @@
   /// any kind (\c \#include, \c \#import, etc.) has been processed, regardless
   /// of whether the inclusion will actually result in an inclusion.
   ///
-  /// \param HashLoc The location of the '#' that starts the inclusion 
+  /// \param HashLoc The location of the '#' that starts the inclusion
   /// directive.
   ///
-  /// \param IncludeTok The token that indicates the kind of inclusion 
+  /// \param IncludeTok The token that indicates the kind of inclusion
   /// directive, e.g., 'include' or 'import'.
   ///
-  /// \param FileName The name of the file being included, as written in the 
+  /// \param FileName The name of the file being included, as written in the
   /// source code.
   ///
   /// \param IsAngled Whether the file name was enclosed in angle brackets;
   /// otherwise, it was enclosed in quotes.
   ///
   /// \param FilenameRange The character range of the quotes or angle brackets
   /// for the written file name.
   ///
-  /// \param File The actual file that may be included by this inclusion 
+  /// \param File The actual file that may be included by this inclusion
   /// directive.
   ///
   /// \param SearchPath Contains the search path which was used to find the file
@@ -117,15 +117,20 @@
   /// \param Imported The module, whenever an inclusion directive was
   /// automatically turned into a module import or null otherwise.
   ///
+  /// \param FileType The characteristic kind, indicates whether a file or
+  /// directory holds normal user code, system code, or system code which is
+  /// implicitly 'extern "C"' in C++ mode.
+  ///
   virtual void InclusionDirective(SourceLocation HashLoc,
                                   const Token &IncludeTok,
                                   StringRef FileName,
                                   bool IsAngled,
                                   CharSourceRange FilenameRange,
                                   const FileEntry *File,
                                   StringRef SearchPath,
                                   StringRef RelativePath,
-                                  const Module *Imported) {
+                                  const Module *Imported,
+                                  SrcMgr::CharacteristicKind FileType) {
   }
 
   /// \brief Callback invoked whenever there was an explicit module-import
@@ -367,13 +372,14 @@
                           StringRef FileName, bool IsAngled,
                           CharSourceRange FilenameRange, const FileEntry *File,
                           StringRef SearchPath, StringRef RelativePath,
-                          const Module *Imported) override {
+                          const Module *Imported,
+                          SrcMgr::CharacteristicKind FileType) override {
     First->InclusionDirective(HashLoc, IncludeTok, FileName, IsAngled,
                               FilenameRange, File, SearchPath, RelativePath,
-                              Imported);
+                              Imported, FileType);
     Second->InclusionDirective(HashLoc, IncludeTok, FileName, IsAngled,
                                FilenameRange, File, SearchPath, RelativePath,
-                               Imported);
+                               Imported, FileType);
   }
 
   void moduleImport(SourceLocation ImportLoc, ModuleIdPath Path,
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to