vsapsai created this revision.
vsapsai added reviewers: arphaman, erik.pilkington, jkorous.
Herald added a subscriber: dexonsmith.

A filename can be remapped with a header map to point to a framework
header and we can find the corresponding framework without the header.
But if the original filename doesn't have a remapped framework name,
we'll fail to find its location and will dereference a null pointer
during diagnostics emission.

Fix by tracking remappings better and emit the note only if a framework
is found before any of the remappings.

rdar://problem/48883447


https://reviews.llvm.org/D61707

Files:
  clang/include/clang/Lex/HeaderSearch.h
  clang/lib/Lex/HeaderSearch.cpp
  
clang/test/Preprocessor/Inputs/include-header-missing-in-framework/TestFramework.hmap.json
  clang/test/Preprocessor/include-header-missing-in-framework-with-headermap.c

Index: clang/test/Preprocessor/include-header-missing-in-framework-with-headermap.c
===================================================================
--- /dev/null
+++ clang/test/Preprocessor/include-header-missing-in-framework-with-headermap.c
@@ -0,0 +1,20 @@
+// RUN: rm -f %t.hmap
+// RUN: %hmaptool write %S/Inputs/include-header-missing-in-framework/TestFramework.hmap.json %t.hmap
+// RUN: %clang_cc1 -fsyntax-only -F %S/Inputs -I %t.hmap -verify %s -DLATE_REMAPPING
+// RUN: %clang_cc1 -fsyntax-only -I %t.hmap -F %S/Inputs -verify %s
+
+// The test is similar to 'include-header-missing-in-framework.c' but covers
+// the case when a header is remapped to a framework-like path with a .hmap
+// file. And we can find the framework but not the header.
+
+#ifdef LATE_REMAPPING
+// Framework is found before remapping.
+#include <TestFramework/BeforeRemapping.h>
+// expected-error@-1 {{'TestFramework/BeforeRemapping.h' file not found}}
+// expected-note@-2 {{did not find header 'BeforeRemapping.h' in framework 'TestFramework' (loaded from}}
+
+#else
+// Framework is found after remapping.
+#include "RemappedHeader.h"
+// expected-error@-1 {{'RemappedHeader.h' file not found}}
+#endif // LATE_REMAPPING
Index: clang/test/Preprocessor/Inputs/include-header-missing-in-framework/TestFramework.hmap.json
===================================================================
--- /dev/null
+++ clang/test/Preprocessor/Inputs/include-header-missing-in-framework/TestFramework.hmap.json
@@ -0,0 +1,7 @@
+{
+  "mappings" :
+    {
+     "RemappedHeader.h" : "TestFramework/RemappedHeader.h",
+     "TestFramework/BeforeRemapping.h" : "TestFramework/AfterRemapping.h"
+    }
+}
Index: clang/lib/Lex/HeaderSearch.cpp
===================================================================
--- clang/lib/Lex/HeaderSearch.cpp
+++ clang/lib/Lex/HeaderSearch.cpp
@@ -818,6 +818,7 @@
   }
 
   CurDir = nullptr;
+  bool HasBeenMapped = false;
 
   // If this is a system #include, ignore the user #include locs.
   unsigned i = isAngled ? AngledDirIdx : 0;
@@ -841,6 +842,7 @@
     i = CacheLookup.HitIdx;
     if (CacheLookup.MappedName) {
       Filename = CacheLookup.MappedName;
+      HasBeenMapped = true;
       if (IsMapped)
         *IsMapped = true;
     }
@@ -856,20 +858,24 @@
   // Check each directory in sequence to see if it contains this file.
   for (; i != SearchDirs.size(); ++i) {
     bool InUserSpecifiedSystemFramework = false;
-    bool HasBeenMapped = false;
+    bool HasBeenMappedInDir = false;
     bool IsFrameworkFoundInDir = false;
     const FileEntry *FE = SearchDirs[i].LookupFile(
         Filename, *this, IncludeLoc, SearchPath, RelativePath, RequestingModule,
         SuggestedModule, InUserSpecifiedSystemFramework, IsFrameworkFoundInDir,
-        HasBeenMapped, MappedName);
-    if (HasBeenMapped) {
+        HasBeenMappedInDir, MappedName);
+    if (HasBeenMappedInDir) {
       CacheLookup.MappedName =
           copyString(Filename, LookupFileCache.getAllocator());
+      HasBeenMapped = true;
       if (IsMapped)
         *IsMapped = true;
     }
     if (IsFrameworkFound)
-      *IsFrameworkFound |= IsFrameworkFoundInDir;
+      // Because we keep a filename remapped for subsequent search directory
+      // lookups, ignore IsFrameworkFoundInDir after the first remapping and not
+      // just for remapping in a current search directory.
+      *IsFrameworkFound |= (IsFrameworkFoundInDir && !HasBeenMapped);
     if (!FE) continue;
 
     CurDir = &SearchDirs[i];
@@ -911,11 +917,10 @@
       return MSFE;
     }
 
-    bool FoundByHeaderMap = !IsMapped ? false : *IsMapped;
     if (!Includers.empty())
       diagnoseFrameworkInclude(Diags, IncludeLoc,
                                Includers.front().second->getName(), Filename,
-                               FE, isAngled, FoundByHeaderMap);
+                               FE, isAngled, HasBeenMapped);
 
     // Remember this location for the next lookup we do.
     CacheLookup.HitIdx = i;
Index: clang/include/clang/Lex/HeaderSearch.h
===================================================================
--- clang/include/clang/Lex/HeaderSearch.h
+++ clang/include/clang/Lex/HeaderSearch.h
@@ -392,8 +392,9 @@
   /// true.
   ///
   /// \param IsFrameworkFound If non-null, will be set to true if a framework is
-  /// found in any of searched SearchDirs. Doesn't guarantee the requested file
-  /// is found.
+  /// found in any of searched SearchDirs. Will be set to false if a framework
+  /// is found only through header maps. Doesn't guarantee the requested file is
+  /// found.
   const FileEntry *LookupFile(
       StringRef Filename, SourceLocation IncludeLoc, bool isAngled,
       const DirectoryLookup *FromDir, const DirectoryLookup *&CurDir,
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to