From 397293f09f7a3e116119ab629687c64aae507539 Mon Sep 17 00:00:00 2001
From: florian <florian@freepascal.org>
Date: Sun, 20 Oct 2024 17:28:20 +0200
Subject: [PATCH]   + more fixes to mark symbols used by preprocessor
 expressions properly   + made MarkSymbolAsUsed more fail safe   + tests

---
 compiler/scanner.pas    | 21 +++++++++++++++------
 tests/test/tpreproc2.pp | 13 +++++++++++++
 tests/test/tpreproc3.pp | 13 +++++++++++++
 tests/test/tpreproc4.pp | 13 +++++++++++++
 tests/test/tpreproc5.pp | 13 +++++++++++++
 tests/test/tpreproc6.pp | 13 +++++++++++++
 6 files changed, 80 insertions(+), 6 deletions(-)
 create mode 100644 tests/test/tpreproc2.pp
 create mode 100644 tests/test/tpreproc3.pp
 create mode 100644 tests/test/tpreproc4.pp
 create mode 100644 tests/test/tpreproc5.pp
 create mode 100644 tests/test/tpreproc6.pp

diff --git a/compiler/scanner.pas b/compiler/scanner.pas
index 02e10ec302..d7dcf4eba9 100644
--- a/compiler/scanner.pas
+++ b/compiler/scanner.pas
@@ -1875,7 +1875,9 @@ type
         procedure MarkSymbolAsUsed(sym: tsym);
           begin
            tabstractvarsym(sym).IncRefCount;
-           inc(current_module.unitmap[sym.owner.moduleid].refs);
+           { do we know an owner? }
+           if Assigned(current_module) and Assigned(current_module.unitmap) and Assigned(sym.owner) then
+             inc(current_module.unitmap[sym.owner.moduleid].refs);
           end;
 
         function preproc_factor(eval: Boolean):texprvalue;
@@ -2107,9 +2109,15 @@ type
                               staticvarsym,
                               localvarsym,
                               paravarsym :
-                                hdef:=tabstractvarsym(srsym).vardef;
+                                begin
+                                  hdef:=tabstractvarsym(srsym).vardef;
+                                  MarkSymbolAsUsed(srsym);
+                                end;
                               typesym:
-                                hdef:=ttypesym(srsym).typedef;
+                                begin
+                                  hdef:=ttypesym(srsym).typedef;
+                                  MarkSymbolAsUsed(srsym);
+                                end;
                               else
                                 Message(scan_e_error_in_preproc_expr);
                             end;
@@ -2206,6 +2214,7 @@ type
                               result:=texprvalue.create_bool(false)
                             else
                               result:=texprvalue.create_bool(true);
+                            MarkSymbolAsUsed(srsym);
                           end
                         else
                           result:=texprvalue.create_bool(false);
@@ -2303,7 +2312,7 @@ type
                                       begin
                                         result.free;
                                         result:=texprvalue.create_const(tconstsym(srsym));
-                                        tconstsym(srsym).IncRefCount;
+                                        MarkSymbolAsUsed(tconstsym(srsym));
                                       end;
                                   end;
                                 enumsym:
@@ -2322,11 +2331,11 @@ type
                                       begin
                                         result.free;
                                         result:=texprvalue.create_int(tenumsym(srsym).value);
-                                        tenumsym(srsym).IncRefCount;
+                                        MarkSymbolAsUsed(tenumsym(srsym));
                                       end;
                                   end;
                                 else
-                                  ;
+                                  MarkSymbolAsUsed(tconstsym(srsym));
                               end;
                           end
                         { the id must be belong to the set type }
diff --git a/tests/test/tpreproc2.pp b/tests/test/tpreproc2.pp
new file mode 100644
index 0000000000..1f71488bae
--- /dev/null
+++ b/tests/test/tpreproc2.pp
@@ -0,0 +1,13 @@
+{ %opt=-vh -Seh }
+program unused;
+
+uses
+	ctypes;
+
+begin
+	{$IF HIGH(cint) >255 )}
+		Writeln('cint is larger than one byte')
+	{$ELSE}
+		Writeln('cint is one byte')
+	{$ENDIF}
+end.
diff --git a/tests/test/tpreproc3.pp b/tests/test/tpreproc3.pp
new file mode 100644
index 0000000000..7fca2517c4
--- /dev/null
+++ b/tests/test/tpreproc3.pp
@@ -0,0 +1,13 @@
+{ %opt=-vh -Seh }
+program unused;
+
+uses
+	types;
+
+begin
+	{$IF CurveKappa > 1 )}
+		Writeln('CurveKappa > 1');
+	{$ELSE}
+		Writeln('CurveKappa <= 1');
+	{$ENDIF}
+end.
diff --git a/tests/test/tpreproc4.pp b/tests/test/tpreproc4.pp
new file mode 100644
index 0000000000..2fa9e53ae3
--- /dev/null
+++ b/tests/test/tpreproc4.pp
@@ -0,0 +1,13 @@
+{ %opt=-vh -Seh }
+program unused;
+
+uses
+	types;
+
+begin
+	{$IF Epsilon > '1' )}
+		Writeln('Epsilon > 1');
+	{$ELSE}
+		Writeln('Epsilon <= 1');
+	{$ENDIF}
+end.
diff --git a/tests/test/tpreproc5.pp b/tests/test/tpreproc5.pp
new file mode 100644
index 0000000000..b586cd8620
--- /dev/null
+++ b/tests/test/tpreproc5.pp
@@ -0,0 +1,13 @@
+{ %opt=-vh -Seh }
+program unused;
+
+uses
+	types;
+
+begin
+	{$IF FromBeginning > 1 )}
+		Writeln('FromBeginning > 1');
+	{$ELSE}
+		Writeln('FromBeginning <= 1');
+	{$ENDIF}
+end.
diff --git a/tests/test/tpreproc6.pp b/tests/test/tpreproc6.pp
new file mode 100644
index 0000000000..2fc88f7c43
--- /dev/null
+++ b/tests/test/tpreproc6.pp
@@ -0,0 +1,13 @@
+{ %opt=-vh -Seh }
+program unused;
+
+uses
+	types;
+
+begin
+	{$IF Declared(Epsilon)}
+		Writeln('Epsilon declared');
+	{$ELSE}
+		Writeln('Epsilon not declared');
+	{$ENDIF}
+end.