DmitryPolukhin created this revision.
DmitryPolukhin added a reviewer: rsmith.
DmitryPolukhin added a subscriber: cfe-commits.

GCC clears current search position in the include directories list used
for #include_next if current header was found using relative paths.
Before this patch Clang kept current position from previous header
included from directories list. Both approaches make sense but I thin it
is better to be GCC compatible in this question. See added test-case for
the example.

Also update warning text to give better understanding what happened.
Previous one about absolute path was incorrect and confusing.

http://reviews.llvm.org/D18641

Files:
  include/clang/Basic/DiagnosticLexKinds.td
  lib/Lex/HeaderSearch.cpp
  lib/Lex/PPDirectives.cpp
  lib/Lex/PPMacroExpansion.cpp
  test/Modules/Inputs/include_next/x/d.h
  test/Modules/Inputs/include_next/y/c.h
  test/Modules/Inputs/include_next/y/d.h
  test/Modules/include_next.c

Index: test/Modules/include_next.c
===================================================================
--- test/Modules/include_next.c
+++ test/Modules/include_next.c
@@ -2,10 +2,14 @@
 // RUN: %clang_cc1 -I%S/Inputs/include_next/x -I%S/Inputs/include_next/y -verify %s
 // RUN: %clang_cc1 -I%S/Inputs/include_next/x -I%S/Inputs/include_next/y -verify %s -fmodules -fimplicit-module-maps -fmodules-cache-path=%t
 
-// expected-no-diagnostics
 #include "a.h"
 #include "subdir/b.h"
+#include <c.h>
+
 _Static_assert(ax == 1, "");
 _Static_assert(ay == 2, "");
 _Static_assert(bx == 3, "");
 _Static_assert(by == 4, "");
+_Static_assert(cy == 5, "");
+_Static_assert(dy == 6, "");
+_Static_assert(dx == 7, "");
Index: test/Modules/Inputs/include_next/y/d.h
===================================================================
--- /dev/null
+++ test/Modules/Inputs/include_next/y/d.h
@@ -0,0 +1,3 @@
+#include_next <d.h>
+// expected-warning@-1 {{#include_next without previous state, searching from the beginning of the include directories list}}
+enum { dy = 6 };
Index: test/Modules/Inputs/include_next/y/c.h
===================================================================
--- /dev/null
+++ test/Modules/Inputs/include_next/y/c.h
@@ -0,0 +1,2 @@
+#include "d.h"
+enum { cy = 5 };
Index: test/Modules/Inputs/include_next/x/d.h
===================================================================
--- /dev/null
+++ test/Modules/Inputs/include_next/x/d.h
@@ -0,0 +1 @@
+enum { dx = 7 };
Index: lib/Lex/PPMacroExpansion.cpp
===================================================================
--- lib/Lex/PPMacroExpansion.cpp
+++ lib/Lex/PPMacroExpansion.cpp
@@ -1415,7 +1415,7 @@
     LookupFromFile = PP.getCurrentLexer()->getFileEntry();
     Lookup = nullptr;
   } else if (!Lookup) {
-    PP.Diag(Tok, diag::pp_include_next_absolute_path);
+    PP.Diag(Tok, diag::pp_include_next_without_state);
   } else {
     // Start looking up in the next directory.
     ++Lookup;
Index: lib/Lex/PPDirectives.cpp
===================================================================
--- lib/Lex/PPDirectives.cpp
+++ lib/Lex/PPDirectives.cpp
@@ -1841,7 +1841,7 @@
     LookupFromFile = CurPPLexer->getFileEntry();
     Lookup = nullptr;
   } else if (!Lookup) {
-    Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
+    Diag(IncludeNextTok, diag::pp_include_next_without_state);
   } else {
     // Start looking up in the next directory.
     ++Lookup;
Index: lib/Lex/HeaderSearch.cpp
===================================================================
--- lib/Lex/HeaderSearch.cpp
+++ lib/Lex/HeaderSearch.cpp
@@ -569,11 +569,13 @@
     bool SkipCache) {
   if (SuggestedModule)
     *SuggestedModule = ModuleMap::KnownHeader();
-    
+
+  // Reset CurDir for subsequent include_next searches. It needs to be done in
+  // both cases absolute patch and ""-include, GCC does the same.
+  CurDir = nullptr;
+
   // If 'Filename' is absolute, check to see if it exists and no searching.
   if (llvm::sys::path::is_absolute(Filename)) {
-    CurDir = nullptr;
-
     // If this was an #include_next "/absolute/file", fail.
     if (FromDir) return nullptr;
 
@@ -673,8 +675,6 @@
     }
   }
 
-  CurDir = nullptr;
-
   // If this is a system #include, ignore the user #include locs.
   unsigned i = isAngled ? AngledDirIdx : 0;
 
Index: include/clang/Basic/DiagnosticLexKinds.td
===================================================================
--- include/clang/Basic/DiagnosticLexKinds.td
+++ include/clang/Basic/DiagnosticLexKinds.td
@@ -265,8 +265,8 @@
   InGroup<DiagGroup<"include-next-outside-header">>;
 def pp_include_macros_out_of_predefines : Error<
   "the #__include_macros directive is only for internal use by -imacros">;
-def pp_include_next_absolute_path : Warning<
-  "#include_next with absolute path">,
+def pp_include_next_without_state : Warning<
+  "#include_next without previous state, searching from the beginning of the include directories list">,
   InGroup<DiagGroup<"include-next-absolute-path">>;
 def ext_c99_whitespace_required_after_macro_name : ExtWarn<
   "ISO C99 requires whitespace after the macro name">, InGroup<C99>;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to