[PATCH] D30882: Add a callback for __has_include and use it for dependency scanning

Pete Cooper via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 13 01:01:45 PDT 2017


pete created this revision.
Herald added a subscriber: nemanjai.

This adds a PP callback for the __has_include and __has_include_next directives.

Checking for the presence of a header should add it to the list of header dependencies so this overrides the callback in the dependency scanner.

I kept the callback intentionally simple for now.  If we want to extend it to track things like missing headers, just as PP->FileNotFound() does today, then that wouldn't be hard to add later.


https://reviews.llvm.org/D30882

Files:
  include/clang/Lex/PPCallbacks.h
  lib/Frontend/DependencyFile.cpp
  lib/Lex/PPMacroExpansion.cpp
  test/Frontend/dependency-gen-has-include.c


Index: test/Frontend/dependency-gen-has-include.c
===================================================================
--- /dev/null
+++ test/Frontend/dependency-gen-has-include.c
@@ -0,0 +1,17 @@
+// REQUIRES: shell
+
+// Basic test
+// RUN: rm -rf %t.dir
+// RUN: mkdir %t.dir
+// RUN: mkdir %t.dir/a
+// RUN: echo "#ifndef HEADER_A" > %t.dir/a/header.h
+// RUN: echo "#define HEADER_A" >> %t.dir/a/header.h
+// RUN: echo "#endif" >> %t.dir/a/header.h
+
+// RUN: %clang -MD -MF %t.dir/file.deps %s -fsyntax-only -I %t.dir
+// RUN: FileCheck -input-file=%t.dir/file.deps %s
+// CHECK: dependency-gen-has-include.o
+// CHECK: dependency-gen-has-include.c
+// CHECK: a/header.h
+#if __has_include("a/header.h")
+#endif
Index: lib/Lex/PPMacroExpansion.cpp
===================================================================
--- lib/Lex/PPMacroExpansion.cpp
+++ lib/Lex/PPMacroExpansion.cpp
@@ -1424,6 +1424,9 @@
       PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, LookupFromFile,
                     CurDir, nullptr, nullptr, nullptr);
 
+  if (PPCallbacks *Callbacks = PP.getPPCallbacks())
+    Callbacks->HasInclude(FilenameLoc, File);
+
   // Get the result value.  A result of true means the file exists.
   return File != nullptr;
 }
Index: lib/Frontend/DependencyFile.cpp
===================================================================
--- lib/Frontend/DependencyFile.cpp
+++ lib/Frontend/DependencyFile.cpp
@@ -196,6 +196,8 @@
                           StringRef SearchPath, StringRef RelativePath,
                           const Module *Imported) override;
 
+  void HasInclude(SourceLocation Loc, const FileEntry *File) override;
+
   void EndOfMainFile() override {
     OutputDependencyFile();
   }
@@ -319,6 +321,12 @@
   }
 }
 
+void DFGImpl::HasInclude(SourceLocation Loc, const FileEntry *File) {
+  if (!File)
+    return;
+  AddFilename(llvm::sys::path::remove_leading_dotslash(File->getName()));
+}
+
 void DFGImpl::AddFilename(StringRef Filename) {
   if (FilesSet.insert(Filename).second)
     Files.push_back(Filename);
Index: include/clang/Lex/PPCallbacks.h
===================================================================
--- include/clang/Lex/PPCallbacks.h
+++ include/clang/Lex/PPCallbacks.h
@@ -258,6 +258,10 @@
   virtual void Defined(const Token &MacroNameTok, const MacroDefinition &MD,
                        SourceRange Range) {
   }
+
+  /// \brief Callback invoked when a has_include directive is read.
+  virtual void HasInclude(SourceLocation Loc, const FileEntry *File) {
+  }
   
   /// \brief Hook called when a source range is skipped.
   /// \param Range The SourceRange that was skipped. The range begins at the
@@ -411,6 +415,11 @@
     Second->PragmaDiagnostic(Loc, Namespace, mapping, Str);
   }
 
+  void HasInclude(SourceLocation Loc, const FileEntry *File) override {
+    First->HasInclude(Loc, File);
+    Second->HasInclude(Loc, File);
+  }
+
   void PragmaOpenCLExtension(SourceLocation NameLoc, const IdentifierInfo *Name,
                              SourceLocation StateLoc, unsigned State) override {
     First->PragmaOpenCLExtension(NameLoc, Name, StateLoc, State);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30882.91520.patch
Type: text/x-patch
Size: 3147 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170313/66db2a41/attachment-0001.bin>


More information about the cfe-commits mailing list