Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-tidy] Create a check that expands TAB characters in code, but not in strnigs #124379

Open
LegalizeAdulthood opened this issue Jan 25, 2025 · 2 comments
Labels
check-request Request for a new check in clang-tidy clang-tidy

Comments

@LegalizeAdulthood
Copy link
Contributor

I'm manually expanding tabs in a code base (git clone https://github.com/LegalizeAdulthood/trn.git; git checkout a34a4b0120ee729aad8a13920fde4af1cf037c05) and it's quite tedious.

First of all, you might think that an IDE would do this for you, but it turns out that Visual Studio will only untabify the indentation at the beginning of a line, so the tabs between statements and comments don't get expanded.

Secondly, you might think "Why don't you use the unix expand command?" and while that's good for files containing plain text, it isn't so good for code that might contain embedded tab characters in string or character literals. For character literals, you don't want the tab character expanded at all, as that would introduce a syntax error. For string literals, you don't want tabs expanded to modulo 8 column (or whatever your tab size is) in the source file because the start of the string literal can be at any column, and you have to worry about the presence of escaped newline characters and so-on. You can't guarantee that expanding the tabs in a string literal is a behavior-preserving transformation.

So what we're left with is the need for a tool that understands the difference between string literals and source code. You might think that clang-format would be the right tool for the job, but I don't think you can get clang-format to leave your code alone EXCEPT for expanding tabs, so we're back to the need for a tool to do this one, single operation.

@LegalizeAdulthood LegalizeAdulthood added check-request Request for a new check in clang-tidy clang-tidy labels Jan 25, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 25, 2025

@llvm/issue-subscribers-clang-tidy

Author: Richard Thomson (LegalizeAdulthood)

I'm manually expanding tabs in a code base (`git clone https://github.com/LegalizeAdulthood/trn.git`; `git checkout a34a4b0120ee729aad8a13920fde4af1cf037c05`) and it's quite tedious.

First of all, you might think that an IDE would do this for you, but it turns out that Visual Studio will only untabify the indentation at the beginning of a line, so the tabs between statements and comments don't get expanded.

Secondly, you might think "Why don't you use the unix expand command?" and while that's good for files containing plain text, it isn't so good for code that might contain embedded tab characters in string or character literals. For character literals, you don't want the tab character expanded at all, as that would introduce a syntax error. For string literals, you don't want tabs expanded to modulo 8 column (or whatever your tab size is) in the source file because the start of the string literal can be at any column, and you have to worry about the presence of escaped newline characters and so-on. You can't guarantee that expanding the tabs in a string literal is a behavior-preserving transformation.

So what we're left with is the need for a tool that understands the difference between string literals and source code. You might think that clang-format would be the right tool for the job, but I don't think you can get clang-format to leave your code alone EXCEPT for expanding tabs, so we're back to the need for a tool to do this one, single operation.

@LegalizeAdulthood
Copy link
Contributor Author

It might also be good to warn about literal TAB characters in character or string literals and suggest \t as a replacement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
check-request Request for a new check in clang-tidy clang-tidy
Projects
None yet
Development

No branches or pull requests

2 participants