From d5a1210aa48c4a77dcee90f203aaed8ef61ae252 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Tue, 10 Apr 2018 08:17:14 -0600 Subject: [PATCH] Write error messages to stderr, renamed `messages` to `errorMessages` --- src/CLI/CLI.cs | 78 ++++++++++++++++++++--------------------- src/Core/Diagnostics.cs | 9 ++++- 2 files changed, 47 insertions(+), 40 deletions(-) diff --git a/src/CLI/CLI.cs b/src/CLI/CLI.cs index e921c20c..3d47bf59 100644 --- a/src/CLI/CLI.cs +++ b/src/CLI/CLI.cs @@ -10,25 +10,25 @@ namespace CppSharp private static OptionSet optionSet = new OptionSet(); private static Options options = new Options(); - static bool ParseCommandLineArgs(string[] args, List messages, ref bool helpShown) + static bool ParseCommandLineArgs(string[] args, List errorMessages, ref bool helpShown) { var showHelp = false; - optionSet.Add("I=", "the {PATH} of a folder to search for include files", (i) => { AddIncludeDirs(i, messages); }); + optionSet.Add("I=", "the {PATH} of a folder to search for include files", (i) => { AddIncludeDirs(i, errorMessages); }); optionSet.Add("l=", "{LIBRARY} that that contains the symbols of the generated code", l => options.Libraries.Add(l) ); optionSet.Add("L=", "the {PATH} of a folder to search for additional libraries", l => options.LibraryDirs.Add(l) ); - optionSet.Add("D:", "additional define with (optional) value to add to be used while parsing the given header files", (n, v) => AddDefine(n, v, messages) ); - optionSet.Add("A=", "additional Clang arguments to pass to the compiler while parsing the given header files", (v) => AddArgument(v, messages) ); + optionSet.Add("D:", "additional define with (optional) value to add to be used while parsing the given header files", (n, v) => AddDefine(n, v, errorMessages) ); + optionSet.Add("A=", "additional Clang arguments to pass to the compiler while parsing the given header files", (v) => AddArgument(v, errorMessages) ); - optionSet.Add("o=|output=", "the {PATH} for the generated bindings file (doesn't need the extension since it will depend on the generator)", v => HandleOutputArg(v, messages) ); + optionSet.Add("o=|output=", "the {PATH} for the generated bindings file (doesn't need the extension since it will depend on the generator)", v => HandleOutputArg(v, errorMessages) ); optionSet.Add("on=|outputnamespace=", "the {NAMESPACE} that will be used for the generated code", on => options.OutputNamespace = on ); optionSet.Add("iln=|inputlibraryname=|inputlib=", "the {NAME} of the shared library that contains the symbols of the generated code", iln => options.InputLibraryName = iln ); optionSet.Add("d|debug", "enables debug mode which generates more verbose code to aid debugging", v => options.Debug = true); optionSet.Add("c|compile", "enables automatic compilation of the generated code", v => options.Compile = true); - optionSet.Add("g=|gen=|generator=", "the {TYPE} of generated code: 'chsarp' or 'cli' ('cli' supported only for Windows)", g => { GetGeneratorKind(g, messages); } ); - optionSet.Add("p=|platform=", "the {PLATFORM} that the generated code will target: 'win', 'osx' or 'linux'", p => { GetDestinationPlatform(p, messages); } ); - optionSet.Add("a=|arch=", "the {ARCHITECTURE} that the generated code will target: 'x86' or 'x64'", a => { GetDestinationArchitecture(a, messages); } ); + optionSet.Add("g=|gen=|generator=", "the {TYPE} of generated code: 'chsarp' or 'cli' ('cli' supported only for Windows)", g => { GetGeneratorKind(g, errorMessages); } ); + optionSet.Add("p=|platform=", "the {PLATFORM} that the generated code will target: 'win', 'osx' or 'linux'", p => { GetDestinationPlatform(p, errorMessages); } ); + optionSet.Add("a=|arch=", "the {ARCHITECTURE} that the generated code will target: 'x86' or 'x64'", a => { GetDestinationArchitecture(a, errorMessages); } ); optionSet.Add("exceptions", "enables support for C++ exceptions in the parser", v => { options.Arguments.Add("-fcxx-exceptions"); }); @@ -59,7 +59,7 @@ namespace CppSharp } foreach(string s in additionalArguments) - HandleAdditionalArgument(s, messages); + HandleAdditionalArgument(s, errorMessages); return true; } @@ -94,15 +94,15 @@ namespace CppSharp Console.WriteLine(" contain only the bindings for that header file."); } - static void AddIncludeDirs(string dir, List messages) + static void AddIncludeDirs(string dir, List errorMessages) { if (Directory.Exists(dir)) options.IncludeDirs.Add(dir); else - messages.Add(string.Format("Directory '{0}' doesn't exist. Ignoring as include directory.", dir)); + errorMessages.Add(string.Format("Directory '{0}' doesn't exist. Ignoring as include directory.", dir)); } - static void HandleOutputArg(string arg, List messages) + static void HandleOutputArg(string arg, List errorMessages) { try { @@ -114,30 +114,30 @@ namespace CppSharp } catch(Exception e) { - messages.Add(e.Message); + errorMessages.Add(e.Message); options.OutputDir = ""; options.OutputFileName = ""; } } - static void AddArgument(string value, List messages) + static void AddArgument(string value, List errorMessages) { if (value == null) - messages.Add("Invalid compiler argument name for option -A."); + errorMessages.Add("Invalid compiler argument name for option -A."); else options.Arguments.Add(value); } - static void AddDefine(string name, string value, List messages) + static void AddDefine(string name, string value, List errorMessages) { if (name == null) - messages.Add("Invalid definition name for option -D."); + errorMessages.Add("Invalid definition name for option -D."); else options.Defines.Add(name, value); } - static void HandleAdditionalArgument(string args, List messages) + static void HandleAdditionalArgument(string args, List errorMessages) { if (!Path.IsPathRooted(args)) args = Path.Combine(Directory.GetCurrentDirectory(), args); @@ -146,21 +146,21 @@ namespace CppSharp { bool searchQuery = args.IndexOf('*') >= 0 || args.IndexOf('?') >= 0; if (searchQuery || Directory.Exists(args)) - GetFilesFromPath(args, messages); + GetFilesFromPath(args, errorMessages); else if (File.Exists(args)) options.HeaderFiles.Add(args); else { - messages.Add(string.Format("File '{0}' could not be found.", args)); + errorMessages.Add(string.Format("File '{0}' could not be found.", args)); } } catch(Exception) { - messages.Add(string.Format("Error while looking for files inside path '{0}'. Ignoring.", args)); + errorMessages.Add(string.Format("Error while looking for files inside path '{0}'. Ignoring.", args)); } } - static void GetFilesFromPath(string path, List messages) + static void GetFilesFromPath(string path, List errorMessages) { path = path.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); @@ -195,11 +195,11 @@ namespace CppSharp } catch (Exception) { - messages.Add(string.Format("Error while looking for files inside path '{0}'. Ignoring.", path)); + errorMessages.Add(string.Format("Error while looking for files inside path '{0}'. Ignoring.", path)); } } - static void GetGeneratorKind(string generator, List messages) + static void GetGeneratorKind(string generator, List errorMessages) { switch (generator.ToLower()) { @@ -211,10 +211,10 @@ namespace CppSharp return; } - messages.Add(string.Format("Unknown generator kind: {0}. Defaulting to {1}", generator, options.Kind.ToString())); + errorMessages.Add(string.Format("Unknown generator kind: {0}. Defaulting to {1}", generator, options.Kind.ToString())); } - static void GetDestinationPlatform(string platform, List messages) + static void GetDestinationPlatform(string platform, List errorMessages) { switch (platform.ToLower()) { @@ -229,10 +229,10 @@ namespace CppSharp return; } - messages.Add(string.Format("Unknown target platform: {0}. Defaulting to {1}", platform, options.Platform.ToString())); + errorMessages.Add(string.Format("Unknown target platform: {0}. Defaulting to {1}", platform, options.Platform.ToString())); } - static void GetDestinationArchitecture(string architecture, List messages) + static void GetDestinationArchitecture(string architecture, List errorMessages) { switch (architecture.ToLower()) { @@ -244,25 +244,25 @@ namespace CppSharp return; } - messages.Add(string.Format("Unknown target architecture: {0}. Defaulting to {1}", architecture, options.Architecture.ToString())); + errorMessages.Add(string.Format("Unknown target architecture: {0}. Defaulting to {1}", architecture, options.Architecture.ToString())); } - static void PrintMessages(List messages) + static void PrintErrorMessages(List errorMessages) { - foreach (string m in messages) - Console.WriteLine(m); + foreach (string m in errorMessages) + Console.Error.WriteLine(m); } static void Main(string[] args) { - List messages = new List(); + List errorMessages = new List(); bool helpShown = false; try { - if (!ParseCommandLineArgs(args, messages, ref helpShown)) + if (!ParseCommandLineArgs(args, errorMessages, ref helpShown)) { - PrintMessages(messages); + PrintErrorMessages(errorMessages); // Don't need to show the help since if ParseCommandLineArgs returns false the help has already been shown return; @@ -270,17 +270,17 @@ namespace CppSharp Generator gen = new Generator(options); - bool validOptions = gen.ValidateOptions(messages); + bool validOptions = gen.ValidateOptions(errorMessages); - PrintMessages(messages); + PrintErrorMessages(errorMessages); if (validOptions) gen.Run(); } catch (Exception ex) { - PrintMessages(messages); - Console.WriteLine(ex.Message); + PrintErrorMessages(errorMessages); + Console.Error.WriteLine(ex.Message); } } } diff --git a/src/Core/Diagnostics.cs b/src/Core/Diagnostics.cs index b462d6d2..3ce0a28c 100644 --- a/src/Core/Diagnostics.cs +++ b/src/Core/Diagnostics.cs @@ -120,7 +120,14 @@ namespace CppSharp var currentIndent = Indents.Sum(); var message = new string(' ', currentIndent) + info.Message; - Console.WriteLine(message); + if(info.Kind == DiagnosticKind.Error) + { + Console.Error.WriteLine(message); + } + else + { + Console.WriteLine(message); + } Debug.WriteLine(message); }