Blame 0001-Fix-isInSystemMacro-to-handle-pasted-macros.patch

06583a2
From cb7fd3caeee52fe94461b717294c4db4056853e3 Mon Sep 17 00:00:00 2001
06583a2
From: Serge Guelton <sguelton@redhat.com>
06583a2
Date: Fri, 1 Feb 2019 06:11:44 +0000
06583a2
Subject: [PATCH 1/3] Fix isInSystemMacro to handle pasted macros
06583a2
06583a2
Token pasted by the preprocessor (through ##) have a Spelling pointing to scratch buffer.
06583a2
As a result they are not recognized at system macro, even though the pasting happened in
06583a2
a system macro. Fix that by looking into the parent macro if the original lookup finds a
06583a2
scratch buffer.
06583a2
06583a2
Differential Revision: https://reviews.llvm.org/D55782
06583a2
06583a2
This effectively fixes https://bugs.llvm.org/show_bug.cgi?id=35268,
06583a2
06583a2
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@352838 91177308-0d34-0410-b5e6-96231b3b80d8
06583a2
---
06583a2
 include/clang/Basic/SourceManager.h      | 18 +++++++++++++++++-
06583a2
 test/Misc/no-warn-in-system-macro.c      | 13 +++++++++++++
06583a2
 test/Misc/no-warn-in-system-macro.c.inc  |  9 +++++++++
06583a2
 test/Misc/warn-in-system-macro-def.c     | 21 +++++++++++++++++++++
06583a2
 test/Misc/warn-in-system-macro-def.c.inc |  4 ++++
06583a2
 5 files changed, 64 insertions(+), 1 deletion(-)
06583a2
 create mode 100644 test/Misc/no-warn-in-system-macro.c
06583a2
 create mode 100644 test/Misc/no-warn-in-system-macro.c.inc
06583a2
 create mode 100644 test/Misc/warn-in-system-macro-def.c
06583a2
 create mode 100644 test/Misc/warn-in-system-macro-def.c.inc
06583a2
06583a2
diff --git a/include/clang/Basic/SourceManager.h b/include/clang/Basic/SourceManager.h
06583a2
index dcc4a37e23..c6b92f9000 100644
06583a2
--- a/include/clang/Basic/SourceManager.h
06583a2
+++ b/include/clang/Basic/SourceManager.h
06583a2
@@ -1441,6 +1441,12 @@ public:
06583a2
     return Filename.equals("<command line>");
06583a2
   }
06583a2
 
06583a2
+  /// Returns whether \p Loc is located in a <scratch space> file.
06583a2
+  bool isWrittenInScratchSpace(SourceLocation Loc) const {
06583a2
+    StringRef Filename(getPresumedLoc(Loc).getFilename());
06583a2
+    return Filename.equals("<scratch space>");
06583a2
+  }
06583a2
+
06583a2
   /// Returns if a SourceLocation is in a system header.
06583a2
   bool isInSystemHeader(SourceLocation Loc) const {
06583a2
     return isSystem(getFileCharacteristic(Loc));
06583a2
@@ -1453,7 +1459,17 @@ public:
06583a2
 
06583a2
   /// Returns whether \p Loc is expanded from a macro in a system header.
06583a2
   bool isInSystemMacro(SourceLocation loc) const {
06583a2
-    return loc.isMacroID() && isInSystemHeader(getSpellingLoc(loc));
06583a2
+    if(!loc.isMacroID())
06583a2
+      return false;
06583a2
+
06583a2
+    // This happens when the macro is the result of a paste, in that case
06583a2
+    // its spelling is the scratch memory, so we take the parent context.
06583a2
+    if (isWrittenInScratchSpace(getSpellingLoc(loc))) {
06583a2
+      return isInSystemHeader(getSpellingLoc(getImmediateMacroCallerLoc(loc)));
06583a2
+    }
06583a2
+    else {
06583a2
+      return isInSystemHeader(getSpellingLoc(loc));
06583a2
+    }
06583a2
   }
06583a2
 
06583a2
   /// The size of the SLocEntry that \p FID represents.
06583a2
diff --git a/test/Misc/no-warn-in-system-macro.c b/test/Misc/no-warn-in-system-macro.c
06583a2
new file mode 100644
06583a2
index 0000000000..a319b14c9c
06583a2
--- /dev/null
06583a2
+++ b/test/Misc/no-warn-in-system-macro.c
06583a2
@@ -0,0 +1,13 @@
06583a2
+// RUN: %clang_cc1 -isystem %S -Wdouble-promotion -fsyntax-only %s  2>&1 | FileCheck -allow-empty %s
06583a2
+// CHECK-NOT: warning:
06583a2
+
06583a2
+#include <no-warn-in-system-macro.c.inc>
06583a2
+
06583a2
+int main(void)
06583a2
+{
06583a2
+	double foo = 1.0;
06583a2
+
06583a2
+	if (isnan(foo))
06583a2
+		return 1;
06583a2
+	return 0;
06583a2
+}
06583a2
diff --git a/test/Misc/no-warn-in-system-macro.c.inc b/test/Misc/no-warn-in-system-macro.c.inc
06583a2
new file mode 100644
06583a2
index 0000000000..3cbe7dfc16
06583a2
--- /dev/null
06583a2
+++ b/test/Misc/no-warn-in-system-macro.c.inc
06583a2
@@ -0,0 +1,9 @@
06583a2
+extern int __isnanf(float f);
06583a2
+extern int __isnan(double f);
06583a2
+extern int __isnanl(long double f);
06583a2
+#define isnan(x) \
06583a2
+	(sizeof (x) == sizeof (float)                \
06583a2
+	? __isnanf (x)                    \
06583a2
+	: sizeof (x) == sizeof (double)               \
06583a2
+	? __isnan (x) : __isnanl (x))
06583a2
+
06583a2
diff --git a/test/Misc/warn-in-system-macro-def.c b/test/Misc/warn-in-system-macro-def.c
06583a2
new file mode 100644
06583a2
index 0000000000..b295130702
06583a2
--- /dev/null
06583a2
+++ b/test/Misc/warn-in-system-macro-def.c
06583a2
@@ -0,0 +1,21 @@
06583a2
+// RUN: %clang_cc1 -isystem %S -Wdouble-promotion -fsyntax-only %s  2>&1 | FileCheck -allow-empty %s
06583a2
+// CHECK: warning:
06583a2
+// CHECK: expanded from macro 'ISNAN'
06583a2
+// CHECK: expanded from macro 'isnan'
06583a2
+
06583a2
+#include <warn-in-system-macro-def.c.inc>
06583a2
+
06583a2
+#define isnan(x) \
06583a2
+	(sizeof (x) == sizeof (float)                \
06583a2
+	? __isnanf (x)                    \
06583a2
+	: sizeof (x) == sizeof (double)               \
06583a2
+	? __isnan (x) : __isnanl (x))
06583a2
+
06583a2
+int main(void)
06583a2
+{
06583a2
+	double foo = 1.0;
06583a2
+
06583a2
+	if (ISNAN(foo))
06583a2
+		return 1;
06583a2
+	return 0;
06583a2
+}
06583a2
diff --git a/test/Misc/warn-in-system-macro-def.c.inc b/test/Misc/warn-in-system-macro-def.c.inc
06583a2
new file mode 100644
06583a2
index 0000000000..5c7e60275a
06583a2
--- /dev/null
06583a2
+++ b/test/Misc/warn-in-system-macro-def.c.inc
06583a2
@@ -0,0 +1,4 @@
06583a2
+extern int __isnanf(float f);
06583a2
+extern int __isnan(double f);
06583a2
+extern int __isnanl(long double f);
06583a2
+#define ISNAN isnan
06583a2
-- 
06583a2
2.20.1
06583a2