Skip to content

Commit

Permalink
C++: handle function-try-block
Browse files Browse the repository at this point in the history
Close universal-ctags#1746.

The original bug report opened by @ChoppinBlockParty:

    void foo() try {
    }catch(...){
    }

"foo" is not captured well.

This commit introduces code ignoring "try" token if the last token is
a pair of "(" and ")".

NOTE: This change doesn't handle well the catch block.  For an
example, a local variable inside the catch block should have "foo" as
"scope". The current implementation cannot recognize it.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
  • Loading branch information
masatake committed Apr 30, 2018
1 parent 54afb0f commit 98ba4ee
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions Units/parser-cxx.r/function_try_block.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--sort=no
4 changes: 4 additions & 0 deletions Units/parser-cxx.r/function_try_block.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
S input.cxx /^struct S {$/;" s file:
m input.cxx /^ std::string m;$/;" m struct:S typeref:typename:std::string file:
S input.cxx /^ S(const std::string& arg) try : m(arg, 100) {$/;" f struct:S file:
f input.cxx /^int f(int n = 2) try {$/;" f typeref:typename:int
20 changes: 20 additions & 0 deletions Units/parser-cxx.r/function_try_block.d/input.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* Taken from
* http://en.cppreference.com/w/cpp/language/function-try-block */

struct S {
std::string m;
S(const std::string& arg) try : m(arg, 100) {
std::cout << "constructed, mn = " << m << '\n';
} catch(const std::exception& e) {
std::cerr << "arg=" << arg << " failed: " << e.what() << '\n';
} // implicit throw; here
};

int f(int n = 2) try {
++n; // increments the function parameter
throw n;
} catch(...) {
++n; // n is in scope and still refers to the function parameter
assert(n == 4);
return n;
}
9 changes: 9 additions & 0 deletions parsers/cxx/cxx_parser_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,15 @@ static bool cxxParserParseBlockInternal(bool bExpectClosingBracket)
cppBeginStatement();
break;
case CXXKeywordTRY:
if (g_cxx.pToken->pPrev
&& cxxTokenTypeIs(g_cxx.pToken->pPrev, CXXTokenTypeParenthesisChain))
{
/* Maybe we are at "try" of "Function-try-block" like
int f(int n = 2) try { ...
Let's ignore this "try". */
continue;
}
/* Fall through */
case CXXKeywordELSE:
case CXXKeywordDO:
// parse as normal statement/block
Expand Down

0 comments on commit 98ba4ee

Please sign in to comment.