Fix #671: Missing newline at end of file should produce a warning#672
Fix #671: Missing newline at end of file should produce a warning#672glankk wants to merge 4 commits into
Conversation
|
All these stray |
I am not sure why the The |
Me neither. Passing less means that the newline would be missing and the other changes add it. That is kind of a flip-flop.
If fiddling with the trailing newline is necessary that seems like a possible source of issues for users of the library. |
The char arrays that are initialized with a string constant include a null terminator, which makes these test cases fail because they end with '\0' instead of '\n'.
I agree that it looks really ugly but it's not wrong. |
|
I also agree that it's a bit awkward to implement this in simplecpp, since it doesn't really distinguish between C and C++. |
Maybe we should something more explicit to highlight what we are doing and it doesn't look like we randomly added those. We should probably land #666 first which allows passing the actual sizes instead of just relying onto |
Of course we should write test code properly. If the newline is not included in the test code then it is not included.
It's not "ugly" to write test code properly. I understand now why you write I.e. if we have |
I want that we are able to find this UB. But we do need to distinguish C/C++ somewhere. I can see these options:
current solution seems OK to me. |
| std::cerr << "portability: "; | ||
| break; | ||
| case simplecpp::Output::PORTABILITY_NO_EOF_NEWLINE: | ||
| std::cerr << "portability: "; |
There was a problem hiding this comment.
we could adjust main.cpp so that this output is only written if the filename extension is ".c".
There was a problem hiding this comment.
I think this is a bit of a clumsy way to solve this issue, I know there exists production code where c++ files have a .c extension (gdb is one example of this). We could have command-line flag for choosing the standard to use for simplecpp, but I think it might be better to just suppress this in the simplecpp executable and let cppcheck choose how to handle this when thrown from the simplecpp library. As far as I know neither the gcc or clang preprocessor have a warning for this, so there's precedence.
There was a problem hiding this comment.
Sorry, I just realized we do have a -std flag for simplecpp. We should probably use that to decide what to do with this warning, and use the filename as a backup for autodetecting the standard to use.
There was a problem hiding this comment.
main.cpp is an "example implementation" more or less. It doesn't have to be perfect. If the -std option has been specified then please rely on that primarily.
Cppcheck has both --std and --language. It has better knowledge about the language than simplecpp.
The --language option is not communicated to simplecpp.
There was a problem hiding this comment.
I suggest something like this:
case simplecpp::Output::PORTABILITY_NO_EOF_NEWLINE:
if (simplecpp::getCStd(dui.std) == simplecpp::CUnknown) {
// Only UB for c code, suppress for c++ code
// If no standard is specified then prefer to have a false negative
continue;
}
std::cerr << "portability: ";
break;
cppcheck will have it's own way of handling this obviously, but I think this is okay for the simplecpp executable.
There was a problem hiding this comment.
I know there exists production code where c++ files have a .c extension
yes sure. And in these cases you would use the cppcheck option --language=c++ .
If you just execute cppcheck --std=c++11 path then you say that c++ files in the path should be analyzed using "c++11". and c files in the path are analyzed with default C standard.
No description provided.