Skip to content

Commit 9c40fe0

Browse files
authored
fixed #10692 - detect Emacs C++ marker in C-style comment blocks (#6511)
1 parent 20472f5 commit 9c40fe0

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

lib/path.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,21 @@ static bool hasEmacsCppMarker(const char* path)
265265
#endif
266266
// TODO: support /* */ comments
267267
const std::string buf_trim = trim(buf); // trim whitespaces
268-
if (buf_trim[0] != '/' || buf_trim[1] != '/') {
268+
if (buf_trim[0] == '/' && buf_trim[1] == '*') {
269+
const auto pos_cmt = buf.find("*/", 2);
270+
if (pos_cmt != std::string::npos && pos_cmt < (pos2 + 3))
271+
{
272+
#ifdef LOG_EMACS_MARKER
273+
std::cout << path << " - Emacs marker not contained in C-style comment block: '" << buf.substr(pos1, (pos2 + 3) - pos1) << "'" << '\n';
274+
#endif
275+
return false; // not in a comment
276+
}
277+
}
278+
else if (buf_trim[0] != '/' || buf_trim[1] != '/') {
269279
#ifdef LOG_EMACS_MARKER
270280
std::cout << path << " - Emacs marker not in a comment: '" << buf.substr(pos1, (pos2 + 3) - pos1) << "'" << '\n';
271281
#endif
272-
return false; // not a comment
282+
return false; // not in a comment
273283
}
274284

275285
// there are more variations with lowercase and no whitespaces

test/testpath.cpp

+23-7
Original file line numberDiff line numberDiff line change
@@ -317,21 +317,26 @@ class TestPath : public TestFixture {
317317
"// -*- c++ -*-",
318318
"// -*- mode: c++; -*-",
319319

320-
//"/* -*- C++ -*- */"
320+
"/* -*- C++ -*- */",
321+
"/* -*- C++ -*-",
321322

322323
"//-*- C++ -*-",
323324
" //-*- C++ -*-",
324325
"\t//-*- C++ -*-",
325326
"\t //-*- C++ -*-",
326327
" \t//-*- C++ -*-",
328+
"//-*- C++ -*- ",
329+
"//-*- C++ -*- \n",
327330
"// -----*- C++ -*-----",
328331
"// comment-*- C++ -*-comment",
332+
"// -*- C++ -*-\n",
329333
"//-*- C++ -*-\r// comment",
330334
"//-*- C++ -*-\n// comment",
331335
"//-*- C++ -*-\r\n// comment",
332336

333-
//"/* -*-C++-*- */"
334-
//"/*-*-C++-*-*/"
337+
"/* -*-C++-*- */",
338+
"/*-*-C++-*-*/",
339+
" /*-*-C++-*-*/",
335340
};
336341

337342
for (const auto& f : { "cppprobe.h", "cppprobe" }) {
@@ -345,15 +350,26 @@ class TestPath : public TestFixture {
345350
"// -*- C++", // no end marker
346351
"// -*- C++ --*-", // incorrect end marker
347352
"// -*- C++/-*-", // unexpected character
348-
"// comment\n// -*-C", // not on the first line
349-
"// comment\r// -*-C", // not on the first line
350-
"// comment\r\n// -*-C", // not on the first line
353+
"// comment\n// -*- C++ -*-", // not on the first line
354+
"// comment\r// -*- C++ -*-", // not on the first line
355+
"// comment\r\n// -*- C++ -*-", // not on the first line
351356
"// -*- C -*-",
352357
"// -*- Mode: C; -*-",
353358
"// -*- f90 -*-",
354359
"// -*- fortran -*-",
355360
"// -*- c-basic-offset: 2 -*-",
356-
"// -*- c-basic-offset:4; indent-tabs-mode:nil -*-"
361+
"// -*- c-basic-offset:4; indent-tabs-mode:nil -*-",
362+
"// ", // no marker
363+
"// -*-", // incomplete marker
364+
"/*", // no marker
365+
"/**/", // no marker
366+
"/*\n*/", // no marker
367+
"/* */", // no marker
368+
"/* \n*/", // no marker
369+
"/* -*-", // incomplete marker
370+
"/* \n-*-", // incomplete marker
371+
"/* \n-*- C++ -*-", // not on the first line
372+
"/* \n-*- C++ -*- */" // not on the first line
357373
};
358374

359375
for (const auto& m : markers_c) {

0 commit comments

Comments
 (0)