[clang] 59dadd1 - [clang][lex] Fix failures with Microsoft header search rules

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 23 06:49:22 PDT 2022


Author: Jan Svoboda
Date: 2022-03-23T14:49:17+01:00
New Revision: 59dadd178b0b12cb4a975a262ed20e7c3822aedc

URL: https://github.com/llvm/llvm-project/commit/59dadd178b0b12cb4a975a262ed20e7c3822aedc
DIFF: https://github.com/llvm/llvm-project/commit/59dadd178b0b12cb4a975a262ed20e7c3822aedc.diff

LOG: [clang][lex] Fix failures with Microsoft header search rules

`HeaderSearch` currently assumes `LookupFileCache` is eventually populated in `LookupFile`. However, that's not always the case with `-fms-compatibility` and its early returns.

This patch adds a defensive check that the iterator pulled out of the cache is actually valid before using it.

(This bug was introduced in D119721. Before that, the cache was initialized to `0` - essentially the `search_dir_begin()` iterator.)

Reviewed By: dexonsmith, erichkeane

Differential Revision: https://reviews.llvm.org/D122237

Added: 
    clang/test/Preprocessor/microsoft-header-search-fail.c

Modified: 
    clang/lib/Lex/HeaderSearch.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp
index 087bd1df860ad..082e62da124f1 100644
--- a/clang/lib/Lex/HeaderSearch.cpp
+++ b/clang/lib/Lex/HeaderSearch.cpp
@@ -976,7 +976,8 @@ Optional<FileEntryRef> HeaderSearch::LookupFile(
   // this is a matching hit.
   if (!SkipCache && CacheLookup.StartIt == NextIt) {
     // Skip querying potentially lots of directories for this lookup.
-    It = CacheLookup.HitIt;
+    if (CacheLookup.HitIt)
+      It = CacheLookup.HitIt;
     if (CacheLookup.MappedName) {
       Filename = CacheLookup.MappedName;
       if (IsMapped)

diff  --git a/clang/test/Preprocessor/microsoft-header-search-fail.c b/clang/test/Preprocessor/microsoft-header-search-fail.c
new file mode 100644
index 0000000000000..1468fc5445b60
--- /dev/null
+++ b/clang/test/Preprocessor/microsoft-header-search-fail.c
@@ -0,0 +1,22 @@
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+
+// RUN: %clang_cc1 -Eonly -fms-compatibility %t/test.c -I %t/include -verify
+
+//--- test.c
+#include "x/header.h"
+#include "z/header.h"
+
+// expected-warning-re at include/y/header.h:1 {{#include resolved using non-portable Microsoft search rules as: {{.*}}x/culprit.h}}
+// expected-error at include/z/header.h:1 {{'culprit.h' file not found}}
+
+//--- include/x/header.h
+#include "y/header.h"
+
+//--- include/y/header.h
+#include "culprit.h"
+
+//--- include/x/culprit.h
+
+//--- include/z/header.h
+#include "culprit.h"


        


More information about the cfe-commits mailing list