From 85c9c83844b494b5422e6ad2fbe8ec1a183d96be Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Mon, 22 Sep 2014 17:17:13 -0700 Subject: [PATCH] Made flatc error messages look like what other compilers output. Looks like MSVC on Windows and like gcc everywhere else. For enhanced IDE clickability. Bug: 17208371 Change-Id: Ie3e02658fccd3edfd464b4bacf4bc68c613a8570 Tested: on Linux and Windows. --- include/flatbuffers/util.h | 23 +++++++++++++++++++++++ src/flatc.cpp | 10 ++++++---- src/idl_parser.cpp | 11 +++++++++-- tests/monster_test.fbs | 2 +- 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/include/flatbuffers/util.h b/include/flatbuffers/util.h index b6bc78ad5..fa3fc4e93 100644 --- a/include/flatbuffers/util.h +++ b/include/flatbuffers/util.h @@ -24,9 +24,18 @@ #include #include #ifdef _WIN32 +#ifndef WIN32_LEAN_AND_MEAN + #define WIN32_LEAN_AND_MEAN +#endif +#ifndef NOMINMAX + #define NOMINMAX +#endif +#include +#include #include #else #include +#include #endif namespace flatbuffers { @@ -157,6 +166,20 @@ inline void EnsureDirExists(const std::string &filepath) { #endif } +// Obtains the absolute path from any other path. +// Returns the input path if the absolute path couldn't be resolved. +inline std::string AbsolutePath(const std::string &filepath) { + #ifdef _WIN32 + char abs_path[MAX_PATH]; + return GetFullPathNameA(filepath.c_str(), MAX_PATH, abs_path, nullptr) + #else + char abs_path[PATH_MAX]; + return realpath(filepath.c_str(), abs_path) + #endif + ? abs_path + : filepath; +} + // To and from UTF-8 unicode conversion functions // Convert a unicode code point into a UTF-8 representation by appending it diff --git a/src/flatc.cpp b/src/flatc.cpp index dc697ec53..f027dbb0a 100755 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -19,7 +19,7 @@ #include "flatbuffers/util.h" static void Error(const char *err, const char *obj = nullptr, - bool usage = false); + bool usage = false, bool show_exe_name = true); namespace flatbuffers { @@ -89,8 +89,10 @@ const Generator generators[] = { const char *program_name = NULL; -static void Error(const char *err, const char *obj, bool usage) { - printf("%s: %s", program_name, err); +static void Error(const char *err, const char *obj, bool usage, + bool show_exe_name) { + if (show_exe_name) printf("%s: ", program_name); + printf("%s", err); if (obj) printf(": %s", obj); printf("\n"); if (usage) { @@ -196,7 +198,7 @@ int main(int argc, const char *argv[]) { include_directories.push_back(nullptr); if (!parser.Parse(contents.c_str(), &include_directories[0], file_it->c_str())) - Error((*file_it + ": " + parser.error_).c_str()); + Error(parser.error_.c_str(), nullptr, false, false); include_directories.pop_back(); include_directories.pop_back(); } diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 03067737c..50973b4a1 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -907,7 +907,7 @@ bool Parser::Parse(const char *source, const char **include_paths, // included_files_. // This is recursive, but only go as deep as the number of include // statements. - return Parse(source, include_paths); + return Parse(source, include_paths, source_filename); } Expect(';'); } @@ -981,7 +981,14 @@ bool Parser::Parse(const char *source, const char **include_paths, } } } catch (const std::string &msg) { - error_ = "line " + NumToString(line_) + ": " + msg; + error_ = source_filename ? AbsolutePath(source_filename) : ""; + #ifdef _WIN32 + error_ += "(" + NumToString(line_) + ")"; // MSVC alike + #else + if (source_filename) error_ += ":"; + error_ += NumToString(line_) + ":0"; // gcc alike + #endif + error_ += ": error: " + msg; return false; } assert(!struct_stack_.size()); diff --git a/tests/monster_test.fbs b/tests/monster_test.fbs index 62dff9e1d..5035252de 100755 --- a/tests/monster_test.fbs +++ b/tests/monster_test.fbs @@ -1,4 +1,4 @@ -// example IDL file +// test schema file include "include_test1.fbs";