Index: OMCompiler/Compiler/.cmake/meta_modelica_source_list.cmake
===================================================================
--- OMCompiler/Compiler/.cmake/meta_modelica_source_list.cmake	(revision None)
+++ OMCompiler/Compiler/.cmake/meta_modelica_source_list.cmake	(revision None)
@@ -318,4 +318,5 @@
     ${CMAKE_CURRENT_SOURCE_DIR}/NFFrontEnd/NFExpression.mo
     ${CMAKE_CURRENT_SOURCE_DIR}/NFFrontEnd/NFFlatModel.mo
+    ${CMAKE_CURRENT_SOURCE_DIR}/NFFrontEnd/NFFlatModelicaUtil.mo
     ${CMAKE_CURRENT_SOURCE_DIR}/NFFrontEnd/NFFlatten.mo
     ${CMAKE_CURRENT_SOURCE_DIR}/NFFrontEnd/NFFunctionDerivative.mo
Index: OMCompiler/Compiler/NFFrontEnd/NFEquation.mo
===================================================================
--- OMCompiler/Compiler/NFFrontEnd/NFEquation.mo	(revision None)
+++ OMCompiler/Compiler/NFFrontEnd/NFEquation.mo	(revision None)
@@ -43,4 +43,5 @@
   import Equation = NFEquation;
   import Error;
+  import FlatModelicaUtil = NFFlatModelicaUtil;
   import IOStream;
   import Util;
@@ -1225,4 +1226,6 @@
       else IOStream.append(s, "#UNKNOWN EQUATION#");
     end match;
+
+    s := FlatModelicaUtil.appendElementSourceComment(source(eq), s);
   end toFlatStream;
 
Index: OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo
===================================================================
--- OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo	(revision None)
+++ OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo	(revision None)
@@ -55,4 +55,5 @@
   import Prefixes = NFPrefixes;
   import NFPrefixes.Visibility;
+  import FlatModelicaUtil = NFFlatModelicaUtil;
 
   import FlatModel = NFFlatModel;
@@ -218,5 +219,7 @@
     Visibility visibility = Visibility.PUBLIC;
   algorithm
-    s := IOStream.append(s, "class '" + flat_model.name + "'\n");
+    s := IOStream.append(s, "class '" + flat_model.name + "'");
+    s := FlatModelicaUtil.appendElementSourceCommentString(flat_model.source, s);
+    s := IOStream.append(s, "\n");
 
     flat_model.variables := reconstructRecordInstances(flat_model.variables);
@@ -269,4 +272,5 @@
     end for;
 
+    s := FlatModelicaUtil.appendElementSourceCommentAnnotation(flat_model.source, "  ", ";\n", s);
     s := IOStream.append(s, "end '" + flat_model.name + "';\n");
   end appendFlatStream;
Index: OMCompiler/Compiler/NFFrontEnd/NFFlatModelicaUtil.mo
===================================================================
--- OMCompiler/Compiler/NFFrontEnd/NFFlatModelicaUtil.mo	(revision None)
+++ OMCompiler/Compiler/NFFrontEnd/NFFlatModelicaUtil.mo	(revision None)
@@ -0,0 +1,212 @@
+/*
+ * This file is part of OpenModelica.
+ *
+ * Copyright (c) 1998-CurrentYear, Linköping University,
+ * Department of Computer and Information Science,
+ * SE-58183 Linköping, Sweden.
+ *
+ * All rights reserved.
+ *
+ * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3
+ * AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
+ * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
+ * ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
+ *
+ * The OpenModelica software and the Open Source Modelica
+ * Consortium (OSMC) Public License (OSMC-PL) are obtained
+ * from Linköping University, either from the above address,
+ * from the URLs: http://www.ida.liu.se/projects/OpenModelica or
+ * http://www.openmodelica.org, and in the OpenModelica distribution.
+ * GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
+ *
+ * This program is distributed WITHOUT ANY WARRANTY; without
+ * even the implied warranty of  MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
+ * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
+ * OF OSMC-PL.
+ *
+ * See the full OSMC Public License conditions for more details.
+ *
+ */
+
+encapsulated package NFFlatModelicaUtil
+  import Absyn;
+  import AbsynUtil;
+  import DAE;
+  import DAEDump;
+  import Dump;
+  import ElementSource;
+  import IOStream;
+  import SCode;
+  import SCodeUtil;
+  import System;
+  import Util;
+
+  function appendElementSourceCommentString
+    input DAE.ElementSource source;
+    input output IOStream.IOStream s;
+  algorithm
+    s := appendCommentString(ElementSource.getOptComment(source), s);
+  end appendElementSourceCommentString;
+
+  function appendElementSourceCommentAnnotation
+    input DAE.ElementSource source;
+    input String indent;
+    input String ending;
+    input output IOStream.IOStream s;
+  algorithm
+    s := appendCommentAnnotation(ElementSource.getOptComment(source), indent, ending, s);
+  end appendElementSourceCommentAnnotation;
+
+  function appendElementSourceComment
+    input DAE.ElementSource source;
+    input output IOStream.IOStream s;
+  algorithm
+    s := appendComment(ElementSource.getOptComment(source), s);
+  end appendElementSourceComment;
+
+  function appendComment
+    input Option<SCode.Comment> comment;
+    input output IOStream.IOStream s;
+  algorithm
+    s := appendCommentString(comment, s);
+    s := appendCommentAnnotation(comment, " ", "", s);
+  end appendComment;
+
+  function appendCommentString
+    input Option<SCode.Comment> comment;
+    input output IOStream.IOStream s;
+  protected
+    String str;
+  algorithm
+    () := match comment
+      case SOME(SCode.Comment.COMMENT(comment = SOME(str)))
+        algorithm
+          s := IOStream.append(s, " \"");
+          s := IOStream.append(s, System.escapedString(str, false));
+          s := IOStream.append(s, "\"");
+        then
+          ();
+
+      else ();
+    end match;
+  end appendCommentString;
+
+  function appendCommentAnnotation
+    input Option<SCode.Comment> comment;
+    input String indent;
+    input String ending;
+    input output IOStream.IOStream s;
+  protected
+    SCode.Mod mod;
+  algorithm
+    () := match comment
+      case SOME(SCode.Comment.COMMENT(annotation_ =
+          SOME(SCode.Annotation.ANNOTATION(modification = mod))))
+        algorithm
+          mod := DAEDump.filterStructuralMods(mod);
+
+          if not SCodeUtil.isEmptyMod(mod) then
+            s := IOStream.append(s, indent);
+            s := IOStream.append(s, "annotation");
+            s := appendAnnotationMod(mod, s);
+            s := IOStream.append(s, ending);
+          end if;
+        then
+          ();
+
+      else ();
+    end match;
+  end appendCommentAnnotation;
+
+  function appendAnnotationMod
+    input SCode.Mod mod;
+    input output IOStream.IOStream s;
+  algorithm
+    () := match mod
+      case SCode.Mod.MOD()
+        algorithm
+          if not listEmpty(mod.subModLst) then
+            s := IOStream.append(s, "(");
+            s := appendAnnotationSubMod(listHead(mod.subModLst), s);
+
+            for m in listRest(mod.subModLst) loop
+              s := IOStream.append(s, ", ");
+              s := appendAnnotationSubMod(m, s);
+            end for;
+
+            s := IOStream.append(s, ")");
+          end if;
+
+          if isSome(mod.binding) then
+            s := IOStream.append(s, " = ");
+            s := appendExp(Util.getOption(mod.binding), s);
+          end if;
+        then
+          ();
+
+      else ();
+    end match;
+  end appendAnnotationMod;
+
+  function appendAnnotationSubMod
+    input SCode.SubMod mod;
+    input output IOStream.IOStream s;
+  protected
+    SCode.Mod m = mod.mod;
+  algorithm
+    () := match m
+      case SCode.Mod.MOD()
+        algorithm
+          if SCodeUtil.finalBool(m.finalPrefix) then
+            s := IOStream.append(s, "final ");
+          end if;
+
+          if SCodeUtil.eachBool(m.eachPrefix) then
+            s := IOStream.append(s, "each ");
+          end if;
+
+          s := IOStream.append(s, mod.ident);
+          s := appendAnnotationMod(m, s);
+        then
+          ();
+
+      else ();
+    end match;
+  end appendAnnotationSubMod;
+
+  function appendExp
+    input Absyn.Exp exp;
+    input output IOStream.IOStream s;
+  protected
+    Absyn.Exp e;
+  algorithm
+    (e, _) := AbsynUtil.traverseExp(exp, quoteCref, 0);
+    s := IOStream.append(s, Dump.printExpStr(e));
+  end appendExp;
+
+  function quoteCref
+    input output Absyn.Exp exp;
+    input output Integer dummy;
+  protected
+    String str;
+  algorithm
+    () := match exp
+      case Absyn.Exp.CREF()
+        guard not AbsynUtil.crefIsWild(exp.componentRef)
+        algorithm
+          str := Dump.printComponentRefStr(exp.componentRef);
+
+          if str <> "time" then
+            str := "'" + str + "'";
+            exp.componentRef := Absyn.ComponentRef.CREF_IDENT(str, {});
+          end if;
+        then
+          ();
+
+      else ();
+    end match;
+  end quoteCref;
+
+annotation(__OpenModelica_Interface="frontend");
+end NFFlatModelicaUtil;
Index: OMCompiler/Compiler/NFFrontEnd/NFFunction.mo
===================================================================
--- OMCompiler/Compiler/NFFrontEnd/NFFunction.mo	(revision None)
+++ OMCompiler/Compiler/NFFrontEnd/NFFunction.mo	(revision None)
@@ -82,4 +82,5 @@
 import UnorderedSet;
 import Graph;
+import FlatModelicaUtil = NFFlatModelicaUtil;
 
 public
@@ -719,9 +720,10 @@
     SCode.Comment cmt;
     SCode.Mod annMod;
-    String str;
   algorithm
     if isDefaultRecordConstructor(fn) then
       s := IOStream.append(s, InstNode.toFlatString(fn.node));
     else
+      cmt := Util.getOptionOrDefault(SCodeUtil.getElementComment(InstNode.definition(fn.node)), SCode.COMMENT(NONE(), NONE()));
+
       fn_name := AbsynUtil.pathString(fn.path);
       if stringEmpty(overrideName) then
@@ -732,4 +734,5 @@
       s := IOStream.append(s, "function ");
       s := IOStream.append(s, fn_name);
+      s := FlatModelicaUtil.appendCommentString(SOME(cmt), s);
       s := IOStream.append(s, "\n");
 
@@ -758,5 +761,4 @@
       s := Sections.toFlatStream(InstNode.getSections(fn.node), fn.path, s);
 
-      cmt := Util.getOptionOrDefault(SCodeUtil.getElementComment(InstNode.definition(fn.node)), SCode.COMMENT(NONE(), NONE()));
       if isSome(cmt.annotation_) then
         SOME(SCode.ANNOTATION(modification=annMod)) := cmt.annotation_;
@@ -777,9 +779,6 @@
 
       if not SCodeUtil.emptyModOrEquality(annMod) then
-        str := SCodeDump.printAnnotationStr(SCode.COMMENT(SOME(SCode.ANNOTATION(annMod)),NONE()));
-        if not stringEmpty(str) then
-          s := IOStream.append(s, str);
-          s := IOStream.append(s, ";\n");
-        end if;
+        cmt := SCode.COMMENT(SOME(SCode.ANNOTATION(annMod)), NONE());
+        s := FlatModelicaUtil.appendCommentAnnotation(SOME(cmt), "  ", ";\n", s);
       end if;
 
Index: OMCompiler/Compiler/NFFrontEnd/NFFunctionDerivative.mo
===================================================================
--- OMCompiler/Compiler/NFFrontEnd/NFFunctionDerivative.mo	(revision None)
+++ OMCompiler/Compiler/NFFrontEnd/NFFunctionDerivative.mo	(revision None)
@@ -173,8 +173,8 @@
     for tpl in fnDer.conditions loop
       (_, id, condition) := tpl;
-      subMods := SCode.NAMEMOD(conditionToString(condition), SCode.MOD(SCode.NOT_FINAL(), SCode.NOT_EACH(), {}, SOME(Absyn.CREF(Absyn.CREF_IDENT(Util.makeQuotedIdentifier(id), {}))), info)) :: subMods;
+      subMods := SCode.NAMEMOD(conditionToString(condition), SCode.MOD(SCode.NOT_FINAL(), SCode.NOT_EACH(), {}, SOME(Absyn.CREF(Absyn.CREF_IDENT(id, {}))), info)) :: subMods;
     end for;
 
-    mod := SCode.MOD(SCode.NOT_FINAL(), SCode.NOT_EACH(), orderMod::subMods, SOME(Absyn.CREF(Absyn.CREF_IDENT(Util.makeQuotedIdentifier(AbsynUtil.pathString(InstNode.scopePath(fnDer.derivativeFn))),{}))), info);
+    mod := SCode.MOD(SCode.NOT_FINAL(), SCode.NOT_EACH(), orderMod::subMods, SOME(Absyn.CREF(Absyn.CREF_IDENT(AbsynUtil.pathString(InstNode.scopePath(fnDer.derivativeFn)),{}))), info);
     subMod := SCode.NAMEMOD("derivative", mod);
   end toSubMod;
Index: OMCompiler/Compiler/NFFrontEnd/NFStatement.mo
===================================================================
--- OMCompiler/Compiler/NFFrontEnd/NFStatement.mo	(revision None)
+++ OMCompiler/Compiler/NFFrontEnd/NFStatement.mo	(revision None)
@@ -39,4 +39,5 @@
   import Statement = NFStatement;
   import ElementSource;
+  import FlatModelicaUtil = NFFlatModelicaUtil;
   import Util;
   import IOStream;
@@ -811,4 +812,5 @@
     end match;
 
+    s := FlatModelicaUtil.appendElementSourceComment(source(stmt), s);
   end toFlatStream;
 
Index: OMCompiler/Compiler/NFFrontEnd/NFVariable.mo
===================================================================
--- OMCompiler/Compiler/NFFrontEnd/NFVariable.mo	(revision None)
+++ OMCompiler/Compiler/NFFrontEnd/NFVariable.mo	(revision None)
@@ -44,4 +44,5 @@
 protected
   import ExpandExp = NFExpandExp;
+  import FlatModelicaUtil = NFFlatModelicaUtil;
   import IOStream;
   import Util;
@@ -339,4 +340,6 @@
       s := IOStream.append(s, Binding.toFlatString(var.binding));
     end if;
+
+    s := FlatModelicaUtil.appendComment(var.comment, s);
   end toFlatStream;
 
Index: OMCompiler/Compiler/boot/LoadCompilerSources.mos
===================================================================
--- OMCompiler/Compiler/boot/LoadCompilerSources.mos	(revision None)
+++ OMCompiler/Compiler/boot/LoadCompilerSources.mos	(revision None)
@@ -352,4 +352,5 @@
     "../NFFrontEnd/NFExpression.mo",
     "../NFFrontEnd/NFFlatModel.mo",
+    "../NFFrontEnd/NFFlatModelicaUtil.mo",
     "../NFFrontEnd/NFFlatten.mo",
     "../NFFrontEnd/NFFunctionDerivative.mo",
Index: testsuite/openmodelica/flatmodelica/Comments.mo
===================================================================
--- testsuite/openmodelica/flatmodelica/Comments.mo	(revision None)
+++ testsuite/openmodelica/flatmodelica/Comments.mo	(revision None)
@@ -0,0 +1,24 @@
+// name: Comments
+// keywords:
+// status: correct
+// cflags: -d=newInst -f
+//
+
+model Comments "Model to test comments in Flat Modelica output"
+  Real x "Some variable" annotation(Evaluate = false);
+  Real y "Some other variable";
+equation
+  x + y = 0 "Some equation" annotation(__A = true);
+  x - y = 1 "Some other equation";
+  annotation(version = "1.0.0");
+end Comments;
+
+// Result:
+// class 'Comments' "Model to test comments in Flat Modelica output"
+//   Real 'x' "Some variable" annotation(Evaluate = false);
+//   Real 'y' "Some other variable";
+// equation
+//   'x' + 'y' = 0.0 "Some equation";
+//   'x' - 'y' = 1.0 "Some other equation";
+// end 'Comments';
+// endResult
Index: testsuite/openmodelica/flatmodelica/Makefile
===================================================================
--- testsuite/openmodelica/flatmodelica/Makefile	(revision None)
+++ testsuite/openmodelica/flatmodelica/Makefile	(revision None)
@@ -11,5 +11,6 @@
 TESTFILES = DoublePendulum.mos \
   Tables.mos \
-	SD.mo
+	SD.mo \
+	Comments.mo
 
 
