From 9c40fe000c6237c7a8ed7302b66e7f8185d20fa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Tue, 11 Jun 2024 16:34:00 +0200 Subject: [PATCH] fixed #10692 - detect Emacs C++ marker in C-style comment blocks (#6511) --- lib/path.cpp | 14 ++++++++++++-- test/testpath.cpp | 30 +++++++++++++++++++++++------- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/lib/path.cpp b/lib/path.cpp index 79af3478247..f7943980924 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -265,11 +265,21 @@ static bool hasEmacsCppMarker(const char* path) #endif // TODO: support /* */ comments const std::string buf_trim = trim(buf); // trim whitespaces - if (buf_trim[0] != '/' || buf_trim[1] != '/') { + if (buf_trim[0] == '/' && buf_trim[1] == '*') { + const auto pos_cmt = buf.find("*/", 2); + if (pos_cmt != std::string::npos && pos_cmt < (pos2 + 3)) + { + #ifdef LOG_EMACS_MARKER + std::cout << path << " - Emacs marker not contained in C-style comment block: '" << buf.substr(pos1, (pos2 + 3) - pos1) << "'" << '\n'; + #endif + return false; // not in a comment + } + } + else if (buf_trim[0] != '/' || buf_trim[1] != '/') { #ifdef LOG_EMACS_MARKER std::cout << path << " - Emacs marker not in a comment: '" << buf.substr(pos1, (pos2 + 3) - pos1) << "'" << '\n'; #endif - return false; // not a comment + return false; // not in a comment } // there are more variations with lowercase and no whitespaces diff --git a/test/testpath.cpp b/test/testpath.cpp index e30470bedc0..5461ebe2b96 100644 --- a/test/testpath.cpp +++ b/test/testpath.cpp @@ -317,21 +317,26 @@ class TestPath : public TestFixture { "// -*- c++ -*-", "// -*- mode: c++; -*-", - //"/* -*- C++ -*- */" + "/* -*- C++ -*- */", + "/* -*- C++ -*-", "//-*- C++ -*-", " //-*- C++ -*-", "\t//-*- C++ -*-", "\t //-*- C++ -*-", " \t//-*- C++ -*-", + "//-*- C++ -*- ", + "//-*- C++ -*- \n", "// -----*- C++ -*-----", "// comment-*- C++ -*-comment", + "// -*- C++ -*-\n", "//-*- C++ -*-\r// comment", "//-*- C++ -*-\n// comment", "//-*- C++ -*-\r\n// comment", - //"/* -*-C++-*- */" - //"/*-*-C++-*-*/" + "/* -*-C++-*- */", + "/*-*-C++-*-*/", + " /*-*-C++-*-*/", }; for (const auto& f : { "cppprobe.h", "cppprobe" }) { @@ -345,15 +350,26 @@ class TestPath : public TestFixture { "// -*- C++", // no end marker "// -*- C++ --*-", // incorrect end marker "// -*- C++/-*-", // unexpected character - "// comment\n// -*-C", // not on the first line - "// comment\r// -*-C", // not on the first line - "// comment\r\n// -*-C", // not on the first line + "// comment\n// -*- C++ -*-", // not on the first line + "// comment\r// -*- C++ -*-", // not on the first line + "// comment\r\n// -*- C++ -*-", // not on the first line "// -*- C -*-", "// -*- Mode: C; -*-", "// -*- f90 -*-", "// -*- fortran -*-", "// -*- c-basic-offset: 2 -*-", - "// -*- c-basic-offset:4; indent-tabs-mode:nil -*-" + "// -*- c-basic-offset:4; indent-tabs-mode:nil -*-", + "// ", // no marker + "// -*-", // incomplete marker + "/*", // no marker + "/**/", // no marker + "/*\n*/", // no marker + "/* */", // no marker + "/* \n*/", // no marker + "/* -*-", // incomplete marker + "/* \n-*-", // incomplete marker + "/* \n-*- C++ -*-", // not on the first line + "/* \n-*- C++ -*- */" // not on the first line }; for (const auto& m : markers_c) {