From 24726c1e0d68eebeaed6bf5f3fd3f39b00a93a7e Mon Sep 17 00:00:00 2001
From: Jozef Lawrynowicz <jozef.l@somniumtech.com>
Date: Fri, 5 May 2017 15:31:39 +0000
Subject: [PATCH 2/2] MSP430: Emit warning when persistent attribute is used on
 an automatic variable

2017-05-XX	Jozef Lawrynowicz	<jozef.l@somniumtech.com>
	
	gcc/
	PR target/78818
	* config/msp430/msp430.c (msp430_unique_section): Warn if .persistent
	attribute is used on an automatic variable.

	gcc/testsuite
	PR target/78818
	* gcc.target/msp430/pr78818/pr78818-auto-warn.c: New test.
---
 gcc/config/msp430/msp430.c                                |  4 ++++
 .../gcc.target/msp430/pr78818/pr78818-auto-warn.c         | 15 +++++++++++++++
 2 files changed, 19 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/msp430/pr78818/pr78818-auto-warn.c

diff --git a/gcc/config/msp430/msp430.c b/gcc/config/msp430/msp430.c
index c70121e..9c3720d 100644
--- a/gcc/config/msp430/msp430.c
+++ b/gcc/config/msp430/msp430.c
@@ -1975,6 +1975,10 @@ msp430_data_attr (tree * node,
   if (DECL_SECTION_NAME (* node))
     message = "%qE attribute cannot be applied to variables with specific sections";
 
+  if (!message && TREE_NAME_EQ (name, ATTR_PERSIST) && !TREE_STATIC (* node)
+      && !TREE_PUBLIC (* node) && !DECL_EXTERNAL (* node))
+    message = "%qE attribute has no effect on automatic variables";
+
   /* It's not clear if there is anything that can be set here to prevent the
    * front end placing the variable before the back end can handle it, in a
    * similar way to how DECL_COMMON is used below.
diff --git a/gcc/testsuite/gcc.target/msp430/pr78818/pr78818-auto-warn.c b/gcc/testsuite/gcc.target/msp430/pr78818/pr78818-auto-warn.c
new file mode 100644
index 0000000..1d92fe1
--- /dev/null
+++ b/gcc/testsuite/gcc.target/msp430/pr78818/pr78818-auto-warn.c
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+
+__attribute__((persistent)) int persistent_1_g = 1;
+__attribute__((persistent)) int persistent_2_g = 0;
+static __attribute__((persistent)) int persistent_3_g = 1;
+static __attribute__((persistent)) int persistent_4_g = 0;
+
+int main (void)
+{
+  __attribute__((persistent)) int persistent_1 = 1; /* { dg-warning "attribute has no effect on automatic" } */
+  __attribute__((persistent)) int persistent_2 = 0; /* { dg-warning "attribute has no effect on automatic" } */
+  static __attribute__((persistent)) int persistent_3 = 1;
+  static __attribute__((persistent)) int persistent_4 = 0;
+  return 0;
+}
-- 
1.8.3.1

