r271185 - clang-format: [JS] Support shebang lines on the very first line.
Martin Probst via cfe-commits
cfe-commits at lists.llvm.org
Sun May 29 07:41:36 PDT 2016
Author: mprobst
Date: Sun May 29 09:41:36 2016
New Revision: 271185
URL: http://llvm.org/viewvc/llvm-project?rev=271185&view=rev
Log:
clang-format: [JS] Support shebang lines on the very first line.
Summary:
Shebang lines (`#!/bin/blah`) can be used in JavaScript scripts to indicate
they should be run using e.g. node. This change treats # lines on the first line
as line comments.
Reviewers: djasper
Subscribers: klimek, cfe-commits
Differential Revision: http://reviews.llvm.org/D20632
Modified:
cfe/trunk/lib/Format/FormatToken.h
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTestJS.cpp
Modified: cfe/trunk/lib/Format/FormatToken.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatToken.h?rev=271185&r1=271184&r2=271185&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatToken.h (original)
+++ cfe/trunk/lib/Format/FormatToken.h Sun May 29 09:41:36 2016
@@ -145,7 +145,7 @@ struct FormatToken {
/// \brief Whether the token text contains newlines (escaped or not).
bool IsMultiline = false;
- /// \brief Indicates that this is the first token.
+ /// \brief Indicates that this is the first token of the file.
bool IsFirst = false;
/// \brief Whether there must be a line break before this token.
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=271185&r1=271184&r2=271185&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Sun May 29 09:41:36 2016
@@ -690,10 +690,24 @@ private:
}
LineType parsePreprocessorDirective() {
+ bool IsFirstToken = CurrentToken->IsFirst;
LineType Type = LT_PreprocessorDirective;
next();
if (!CurrentToken)
return Type;
+
+ if (Style.Language == FormatStyle::LK_JavaScript && IsFirstToken) {
+ // JavaScript files can contain shebang lines of the form:
+ // #!/usr/bin/env node
+ // Treat these like C++ #include directives.
+ while (CurrentToken) {
+ // Tokens cannot be comments here.
+ CurrentToken->Type = TT_ImplicitStringLiteral;
+ next();
+ }
+ return LT_ImportStatement;
+ }
+
if (CurrentToken->Tok.is(tok::numeric_constant)) {
CurrentToken->SpacesRequiredBefore = 1;
return Type;
Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=271185&r1=271184&r2=271185&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Sun May 29 09:41:36 2016
@@ -1276,5 +1276,12 @@ TEST_F(FormatTestJS, RequoteStringsLeave
verifyFormat("var x = 'foo';", LeaveQuotes);
}
+TEST_F(FormatTestJS, SupportShebangLines) {
+ verifyFormat("#!/usr/bin/env node\n"
+ "var x = hello();",
+ "#!/usr/bin/env node\n"
+ "var x = hello();");
+}
+
} // end namespace tooling
} // end namespace clang
More information about the cfe-commits
mailing list