This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] [PR46159] Linux kernel 'C' code uses 'try' as a variable name, allow clang-format to handle such cases
ClosedPublic

Authored by MyDeveloperDay on Jun 1 2020, 11:11 AM.

Details

Summary

https://bugs.llvm.org/show_bug.cgi?id=46159

Linux Kernel code using is try as a variable name (as its 'C' code they can), they interpreted as the 'try' keyword by clang-format causing odd formatting (see below)

We have no specific 'C' code handling, as this code could easily be in a '.h' file so need to handle this not based on extension alone.

This revision adds a FormatTokenLexer rule to identify either try blocks "try {" or function try block "try :" and if not, rename the "try" token to be an identifier

static size_t get_kcore_size(int *nphdr, size_t *phdrs_len, size_t *notes_len,
			     size_t *data_offset)
{
	size_t try, size;
	struct kcore_list *m;

	*nphdr = 1; /* PT_NOTE */
	size = 0;

	list_for_each_entry(m, &kclist_head, list) {
		try = kc_vaddr_to_offset((size_t)m->addr + m->size);
		if (try1 > size)
			size = try1;
		*nphdr = *nphdr + 1;
	}
}

clang-format-10 formats it as:

static size_t get_kcore_size(int *nphdr, size_t *phdrs_len, size_t *notes_len,
                             size_t *data_offset) {
  size_t try
    , size;
  struct kcore_list *m;

  *nphdr = 1; /* PT_NOTE */
  size = 0;

  list_for_each_entry(m, &kclist_head, list) {
    try
      = kc_vaddr_to_offset((size_t)m->addr + m->size);
    if (try > size)
      size = try
        ;
    *nphdr = *nphdr + 1;
  }
}

Diff Detail

Event Timeline

MyDeveloperDay created this revision.Jun 1 2020, 11:11 AM

Nice. Should we test other non-C keywords as well?
Out of curiosity, where does "@try" come from?

clang/lib/Format/FormatTokenLexer.cpp
392

Please rename to something that includes colon or is more general. Just using Brace is misleading.

393

Nit: You can move this if above.

Out of curiosity, where does "@try" come from?

Objective C++ I think

Address review comments

MyDeveloperDay marked 2 inline comments as done.Jun 3 2020, 6:11 AM
MyDeveloperDay added a reviewer: curdeius.
curdeius accepted this revision.Jun 3 2020, 6:52 AM

LGTM.

For a second, I thought that you could simplify the code by removing this @try condition (and calling the function FormatTokenLexer::tryTransformTryUsageForC() only if isCppOnly, but given that Objective is a superset of C, it's probably safer to keep it the way it's done now.

This revision is now accepted and ready to land.Jun 3 2020, 6:52 AM
MyDeveloperDay added a comment.EditedJun 3 2020, 10:40 AM

LGTM.

For a second, I thought that you could simplify the code by removing this @try condition (and calling the function FormatTokenLexer::tryTransformTryUsageForC() only if isCppOnly, but given that Objective is a superset of C, it's probably safer to keep it the way it's done now.

this is the premise of D80079: [clang-format] [NFC] isCpp() is inconsistently used to mean both C++ and Objective C, add language specific isXXX() functions but as we now are learning isCpp() is not what is says on the tin!

This revision was automatically updated to reflect the committed changes.