From a554753db631ceb39e96a37b1e4538722ac95cb7 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Mon, 15 Apr 2019 12:42:29 +0200
Subject: [PATCH] BUG: bad '#line' directives for dynamicCode (fixes #1282)

- now suppress any '#line' if the input number number is invalid
  (ie, an empty set of tokens)
---
 .../dynamicLibrary/dynamicCode/dynamicCodeContext.C   | 11 ++++++++---
 .../dynamicLibrary/dynamicCode/dynamicCodeContext.H   |  9 +++++----
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.C b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.C
index 79106b07331..602b7137750 100644
--- a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.C
+++ b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011-2011 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2011-2011, 2019 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
                             | Copyright (C) 2011-2016 OpenFOAM Foundation
@@ -124,11 +124,16 @@ Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& dict)
 void Foam::dynamicCodeContext::addLineDirective
 (
     string& code,
-    const label lineNum,
+    label lineNum,
     const fileName& name
 )
 {
-    code = "#line " + Foam::name(lineNum + 1) + " \"" + name + "\"\n" + code;
+    ++lineNum;  // Change from 0-based to 1-based
+
+    if (lineNum > 0 && !name.empty())
+    {
+        code = "#line " + Foam::name(lineNum) + " \"" + name + "\"\n" + code;
+    }
 }
 
 
diff --git a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.H b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.H
index dde6695a322..7458bcb2ab1 100644
--- a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.H
+++ b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011-2011 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2011-2011, 2019 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
                             | Copyright (C) 2011-2016 OpenFOAM Foundation
@@ -80,7 +80,7 @@ public:
     // Constructors
 
         //- Construct from a dictionary
-        dynamicCodeContext(const dictionary&);
+        dynamicCodeContext(const dictionary& dict);
 
 
     // Member functions
@@ -128,10 +128,11 @@ public:
         }
 
         //- Helper: add \#line directive
+        //  The lineNum is 0-based. No-op if the lineNum is negative.
         static void addLineDirective
         (
-            string&,
-            const label lineNum,
+            string& code,
+            label lineNum,
             const fileName& name
         );
 };
-- 
GitLab