[hawkmoth] [PATCH] parser: take into account clang diagnostics without a file

  • From: Jani Nikula <jani@xxxxxxxxxx>
  • To: hawkmoth@xxxxxxxxxxxxx
  • Date: Mon, 25 Jan 2021 21:57:06 +0200

Not all clang translation unit diagnostics are associated with a
file. For example, incorrect clang parameters have None for diagnostics
location. Handle them gracefully.

Fixes: #28
---
 hawkmoth/__init__.py | 5 ++++-
 hawkmoth/__main__.py | 7 +++++--
 hawkmoth/parser.py   | 3 ++-
 3 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/hawkmoth/__init__.py b/hawkmoth/__init__.py
index 5905c2b53f00..9cfbc0ab0a20 100644
--- a/hawkmoth/__init__.py
+++ b/hawkmoth/__init__.py
@@ -52,7 +52,10 @@ class CAutoDocDirective(Directive):
         env = self.state.document.settings.env
 
         for (severity, filename, lineno, msg) in errors:
-            toprint = '{}:{}: {}'.format(filename, lineno, msg)
+            if filename:
+                toprint = '{}:{}: {}'.format(filename, lineno, msg)
+            else:
+                toprint = '{}'.format(msg)
 
             if severity.value <= env.app.verbosity:
                 self.logger.log(self._log_lvl[severity], toprint,
diff --git a/hawkmoth/__main__.py b/hawkmoth/__main__.py
index 96f6ce7f25fc..25029a508f3f 100644
--- a/hawkmoth/__main__.py
+++ b/hawkmoth/__main__.py
@@ -39,7 +39,10 @@ def main():
         print(doc)
 
     for (severity, filename, lineno, msg) in errors:
-        print('{}: {}:{}: {}'.format(severity.name,
-                                     filename, lineno, msg), file=sys.stderr)
+        if filename:
+            print('{}: {}:{}: {}'.format(severity.name,
+                                         filename, lineno, msg), 
file=sys.stderr)
+        else:
+            print('{}: {}'.format(severity.name, msg), file=sys.stderr)
 
 main()
diff --git a/hawkmoth/parser.py b/hawkmoth/parser.py
index 37862d38327c..430042bfd2ad 100644
--- a/hawkmoth/parser.py
+++ b/hawkmoth/parser.py
@@ -278,7 +278,8 @@ def clang_diagnostics(errors, diagnostics):
            4: ErrorLevel.ERROR}
 
     for diag in diagnostics:
-        errors.extend([(sev[diag.severity], diag.location.file.name,
+        filename = diag.location.file.name if diag.location.file else None
+        errors.extend([(sev[diag.severity], filename,
                         diag.location.line, diag.spelling)])
 
 # return a list of (comment, metadata) tuples
-- 
2.29.2


Other related posts: