From 282f05ed383cda42e844d8b4b510cf7e80e25721 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Sun, 13 Nov 2022 21:18:17 +0100 Subject: [PATCH] Handle circular includes Fixes #150. --- src/libclang/preprocessor.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/libclang/preprocessor.cpp b/src/libclang/preprocessor.cpp index 8d63ee5..7bede9e 100644 --- a/src/libclang/preprocessor.cpp +++ b/src/libclang/preprocessor.cpp @@ -506,7 +506,15 @@ class position public: position(ts::object_ref result, const char* ptr) noexcept : result_(result), cur_line_(1u), cur_column_(0u), ptr_(ptr), write_(true) - {} + { + // We strip all conditional defines and pragmas from the input, which includes the include + // guard. If the source includes a file, which includes itself again (for some reason), this + // leads to a duplicate include, as we no longer have an include guard. So we manually add + // one. + *result += "#pragma once\n"; + // We also need to reset the line afterwards to ensure comments still match. + *result += "#line 1\n"; + } void set_line(unsigned line) {