Skip to content

Commit

Permalink
fixed #10692 - detect Emacs C++ marker in C-style comment blocks (#6511)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave authored Jun 11, 2024
1 parent 20472f5 commit 9c40fe0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
14 changes: 12 additions & 2 deletions lib/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 23 additions & 7 deletions test/testpath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" }) {
Expand All @@ -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) {
Expand Down

0 comments on commit 9c40fe0

Please sign in to comment.