From 28b6820026dea80d78e7da4d874e93fe1e79733f Mon Sep 17 00:00:00 2001
From: Nikolai Kosjar <nikolai.kosjar@qt.io>
Date: Thu, 21 Apr 2016 16:32:07 +0200
Subject: [PATCH] Clang: Print error code on parse error

Change-Id: Idecb0e9b78cc14c603de09fc460bbf17f43bb451
Reviewed-by: Christian Stenger <christian.stenger@theqtcompany.com>
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
---
 .../ipcsource/clangtranslationunit.cpp        |  4 ++-
 .../translationunitparseerrorexception.cpp    | 29 ++++++++++++++++---
 .../translationunitparseerrorexception.h      |  7 ++++-
 3 files changed, 34 insertions(+), 6 deletions(-)

diff --git a/src/tools/clangbackend/ipcsource/clangtranslationunit.cpp b/src/tools/clangbackend/ipcsource/clangtranslationunit.cpp
index 38315719d1c..3460e6ff46d 100644
--- a/src/tools/clangbackend/ipcsource/clangtranslationunit.cpp
+++ b/src/tools/clangbackend/ipcsource/clangtranslationunit.cpp
@@ -417,7 +417,9 @@ void TranslationUnit::checkTranslationUnitErrorCode(CXErrorCode errorCode) const
 {
     switch (errorCode) {
         case CXError_Success: break;
-        default: throw TranslationUnitParseErrorException(d->filePath, d->projectPart.projectPartId());
+        default: throw TranslationUnitParseErrorException(d->filePath,
+                                                          d->projectPart.projectPartId(),
+                                                          errorCode);
     }
 }
 
diff --git a/src/tools/clangbackend/ipcsource/translationunitparseerrorexception.cpp b/src/tools/clangbackend/ipcsource/translationunitparseerrorexception.cpp
index efb3c56ff93..3cd99c53e8a 100644
--- a/src/tools/clangbackend/ipcsource/translationunitparseerrorexception.cpp
+++ b/src/tools/clangbackend/ipcsource/translationunitparseerrorexception.cpp
@@ -27,9 +27,13 @@
 
 namespace ClangBackEnd {
 
-TranslationUnitParseErrorException::TranslationUnitParseErrorException(const Utf8String &filePath, const Utf8String &projectPartId)
+TranslationUnitParseErrorException::TranslationUnitParseErrorException(
+        const Utf8String &filePath,
+        const Utf8String &projectPartId,
+        CXErrorCode errorCode)
     : filePath_(filePath),
-      projectPartId_(projectPartId)
+      projectPartId_(projectPartId),
+      errorCode_(errorCode)
 {
 }
 
@@ -43,14 +47,31 @@ const Utf8String &TranslationUnitParseErrorException::projectPartId() const
     return projectPartId_;
 }
 
+#define RETURN_TEXT_FOR_CASE(enumValue) case enumValue: return #enumValue
+static const char *errorCodeToText(CXErrorCode errorCode)
+{
+    switch (errorCode) {
+        RETURN_TEXT_FOR_CASE(CXError_Success);
+        RETURN_TEXT_FOR_CASE(CXError_Failure);
+        RETURN_TEXT_FOR_CASE(CXError_Crashed);
+        RETURN_TEXT_FOR_CASE(CXError_InvalidArguments);
+        RETURN_TEXT_FOR_CASE(CXError_ASTReadError);
+    }
+
+    return "UnknownCXErrorCode";
+}
+#undef RETURN_TEXT_FOR_CASE
+
 const char *TranslationUnitParseErrorException::what() const Q_DECL_NOEXCEPT
 {
-    if (what_.isEmpty())
+    if (what_.isEmpty()) {
         what_ += Utf8StringLiteral("Parse error for file ")
                 + filePath()
                 + Utf8StringLiteral(" in project ")
                 + projectPartId()
-                + Utf8StringLiteral("!");
+                + Utf8StringLiteral(": ")
+                + Utf8String::fromUtf8(errorCodeToText(errorCode_));
+    }
 
     return what_.constData();
 }
diff --git a/src/tools/clangbackend/ipcsource/translationunitparseerrorexception.h b/src/tools/clangbackend/ipcsource/translationunitparseerrorexception.h
index 8175fa74547..e1b949f2c9e 100644
--- a/src/tools/clangbackend/ipcsource/translationunitparseerrorexception.h
+++ b/src/tools/clangbackend/ipcsource/translationunitparseerrorexception.h
@@ -28,6 +28,8 @@
 
 #include <utf8string.h>
 
+#include <clang-c/Index.h>
+
 #include <exception>
 
 namespace ClangBackEnd {
@@ -35,7 +37,9 @@ namespace ClangBackEnd {
 class TranslationUnitParseErrorException : public std::exception
 {
 public:
-    TranslationUnitParseErrorException(const Utf8String &filePath, const Utf8String &projectPartId);
+    TranslationUnitParseErrorException(const Utf8String &filePath,
+                                       const Utf8String &projectPartId,
+                                       CXErrorCode errorCode);
 
     const Utf8String &filePath() const;
     const Utf8String &projectPartId() const;
@@ -51,6 +55,7 @@ public:
 private:
     Utf8String filePath_;
     Utf8String projectPartId_;
+    CXErrorCode errorCode_;
     mutable Utf8String what_;
 };
 
-- 
GitLab