diff options
| -rw-r--r-- | clang/lib/Format/FormatToken.h | 2 | ||||
| -rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 14 | ||||
| -rw-r--r-- | clang/unittests/Format/FormatTestJS.cpp | 7 |
3 files changed, 22 insertions, 1 deletions
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h index 84a7e4153a0..60b97f59874 100644 --- a/clang/lib/Format/FormatToken.h +++ b/clang/lib/Format/FormatToken.h @@ -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. diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index e06b5de419d..a8b79b22125 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -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; diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 33fd065ce49..e956ae24424 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -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 |

