From 35e57ca748398edf569d357a7ca192ecc5fd8d5e Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Mon, 1 Nov 2021 14:44:32 +0100 Subject: [PATCH] ClangCodeModel: Make sure not to overwrite macros with their expansion ... when highlighting with clangd. Change-Id: I89ec8bf5a1ed4d43e0e9a398b26705df00ca9a46 Reviewed-by: David Schulz --- src/plugins/clangcodemodel/clangdclient.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/plugins/clangcodemodel/clangdclient.cpp b/src/plugins/clangcodemodel/clangdclient.cpp index 17fe2775074..09f9cedd191 100644 --- a/src/plugins/clangcodemodel/clangdclient.cpp +++ b/src/plugins/clangcodemodel/clangdclient.cpp @@ -3140,6 +3140,18 @@ void ExtraHighlightingResultsCollector::insertResult(const HighlightingResult &r return; const auto it = std::lower_bound(m_results.begin(), m_results.end(), result, lessThan); if (it == m_results.end() || *it != result) { + + // Prevent inserting expansions for function-like macros. For instance: + // #define TEST() "blubb" + // const char *s = TEST(); + // The macro name is always shorter than the expansion and starts at the same + // location, so it should occur right before the insertion position. + if (it > m_results.begin() && (it - 1)->line == result.line + && (it - 1)->column == result.column + && (it - 1)->textStyles.mainStyle == C_PREPROCESSOR) { + return; + } + qCDebug(clangdLogHighlight) << "adding additional highlighting result" << result.line << result.column << result.length; m_results.insert(it, result);