[COMMITTED] ada: Reject misplaced pragma Obsolescent

Message ID 20221107083913.150332-1-poulhies@adacore.com
State Repeat Merge
Headers
Series [COMMITTED] ada: Reject misplaced pragma Obsolescent |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Marc Poulhiès Nov. 7, 2022, 8:39 a.m. UTC
  From: Piotr Trojanek <trojanek@adacore.com>

Pragma Obsolescent appearing before declaration was putting the
Obsolescent flag on the Standard package, which is certainly wrong. The
problem was that we relied on the Find_Lib_Unit_Name routine without
sanitizing the pragma placement with Check_Valid_Library_Unit_Pragma.

Part of cleaning up the warnings machinery to better handle references
to unset objects.

gcc/ada/

	* sem_prag.adb (Analyze_Pragma [Pragma_Obsolescent]): Reject
	misplaced pragma.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/sem_prag.adb | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
  

Patch

diff --git a/gcc/ada/sem_prag.adb b/gcc/ada/sem_prag.adb
index 60ea681001a..471ef870fa7 100644
--- a/gcc/ada/sem_prag.adb
+++ b/gcc/ada/sem_prag.adb
@@ -20502,10 +20502,16 @@  package body Sem_Prag is
 
             if No (Decl) then
 
-               --  First case: library level compilation unit declaration with
+               --  Case 0: library level compilation unit declaration with
+               --  the pragma preceding the declaration.
+
+               if Nkind (Parent (N)) = N_Compilation_Unit then
+                  Pragma_Misplaced;
+
+               --  Case 1: library level compilation unit declaration with
                --  the pragma immediately following the declaration.
 
-               if Nkind (Parent (N)) = N_Compilation_Unit_Aux then
+               elsif Nkind (Parent (N)) = N_Compilation_Unit_Aux then
                   Set_Obsolescent
                     (Defining_Entity (Unit (Parent (Parent (N)))));
                   return;