| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| add_subdirectory(ClangCommon) | ||
| add_subdirectory(CPlusPlus) | ||
| add_subdirectory(Go) | ||
| add_subdirectory(Java) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| add_lldb_library(lldbPluginClangCommon PLUGIN | ||
| ClangHighlighter.cpp | ||
|
|
||
| LINK_LIBS | ||
| lldbCore | ||
| lldbUtility | ||
| LINK_COMPONENTS | ||
| Support | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| //===-- ClangHighlighter.cpp ------------------------------------*- C++ -*-===// | ||
| // | ||
| // The LLVM Compiler Infrastructure | ||
| // | ||
| // This file is distributed under the University of Illinois Open Source | ||
| // License. See LICENSE.TXT for details. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "ClangHighlighter.h" | ||
|
|
||
| #include "lldb/Target/Language.h" | ||
| #include "lldb/Utility/AnsiTerminal.h" | ||
| #include "lldb/Utility/StreamString.h" | ||
|
|
||
| #include "clang/Basic/SourceManager.h" | ||
| #include "clang/Lex/Lexer.h" | ||
| #include "llvm/ADT/StringSet.h" | ||
| #include "llvm/Support/MemoryBuffer.h" | ||
|
|
||
| using namespace lldb_private; | ||
|
|
||
| bool ClangHighlighter::isKeyword(llvm::StringRef token) const { | ||
| return keywords.find(token) != keywords.end(); | ||
| } | ||
|
|
||
| ClangHighlighter::ClangHighlighter() { | ||
| #define KEYWORD(X, N) keywords.insert(#X); | ||
| #include "clang/Basic/TokenKinds.def" | ||
| } | ||
|
|
||
| /// Determines which style should be applied to the given token. | ||
| /// \param highlighter | ||
| /// The current highlighter that should use the style. | ||
| /// \param token | ||
| /// The current token. | ||
| /// \param tok_str | ||
| /// The string in the source code the token represents. | ||
| /// \param options | ||
| /// The style we use for coloring the source code. | ||
| /// \param in_pp_directive | ||
| /// If we are currently in a preprocessor directive. NOTE: This is | ||
| /// passed by reference and will be updated if the current token starts | ||
| /// or ends a preprocessor directive. | ||
| /// \return | ||
| /// The ColorStyle that should be applied to the token. | ||
| static HighlightStyle::ColorStyle | ||
| determineClangStyle(const ClangHighlighter &highlighter, | ||
| const clang::Token &token, llvm::StringRef tok_str, | ||
| const HighlightStyle &options, bool &in_pp_directive) { | ||
| using namespace clang; | ||
|
|
||
| if (token.is(tok::comment)) { | ||
| // If we were in a preprocessor directive before, we now left it. | ||
| in_pp_directive = false; | ||
| return options.comment; | ||
| } else if (in_pp_directive || token.getKind() == tok::hash) { | ||
| // Let's assume that the rest of the line is a PP directive. | ||
| in_pp_directive = true; | ||
| // Preprocessor directives are hard to match, so we have to hack this in. | ||
| return options.pp_directive; | ||
| } else if (tok::isStringLiteral(token.getKind())) | ||
| return options.string_literal; | ||
| else if (tok::isLiteral(token.getKind())) | ||
| return options.scalar_literal; | ||
| else if (highlighter.isKeyword(tok_str)) | ||
| return options.keyword; | ||
| else | ||
| switch (token.getKind()) { | ||
| case tok::raw_identifier: | ||
| case tok::identifier: | ||
| return options.identifier; | ||
| case tok::l_brace: | ||
| case tok::r_brace: | ||
| return options.braces; | ||
| case tok::l_square: | ||
| case tok::r_square: | ||
| return options.square_brackets; | ||
| case tok::l_paren: | ||
| case tok::r_paren: | ||
| return options.parentheses; | ||
| case tok::comma: | ||
| return options.comma; | ||
| case tok::coloncolon: | ||
| case tok::colon: | ||
| return options.colon; | ||
|
|
||
| case tok::amp: | ||
| case tok::ampamp: | ||
| case tok::ampequal: | ||
| case tok::star: | ||
| case tok::starequal: | ||
| case tok::plus: | ||
| case tok::plusplus: | ||
| case tok::plusequal: | ||
| case tok::minus: | ||
| case tok::arrow: | ||
| case tok::minusminus: | ||
| case tok::minusequal: | ||
| case tok::tilde: | ||
| case tok::exclaim: | ||
| case tok::exclaimequal: | ||
| case tok::slash: | ||
| case tok::slashequal: | ||
| case tok::percent: | ||
| case tok::percentequal: | ||
| case tok::less: | ||
| case tok::lessless: | ||
| case tok::lessequal: | ||
| case tok::lesslessequal: | ||
| case tok::spaceship: | ||
| case tok::greater: | ||
| case tok::greatergreater: | ||
| case tok::greaterequal: | ||
| case tok::greatergreaterequal: | ||
| case tok::caret: | ||
| case tok::caretequal: | ||
| case tok::pipe: | ||
| case tok::pipepipe: | ||
| case tok::pipeequal: | ||
| case tok::question: | ||
| case tok::equal: | ||
| case tok::equalequal: | ||
| return options.operators; | ||
| default: | ||
| break; | ||
| } | ||
| return HighlightStyle::ColorStyle(); | ||
| } | ||
|
|
||
| std::size_t ClangHighlighter::Highlight(const HighlightStyle &options, | ||
| llvm::StringRef line, | ||
| llvm::StringRef previous_lines, | ||
| Stream &result) const { | ||
| using namespace clang; | ||
|
|
||
| std::size_t written_bytes = 0; | ||
|
|
||
| FileSystemOptions file_opts; | ||
| FileManager file_mgr(file_opts); | ||
|
|
||
| unsigned line_number = previous_lines.count('\n') + 1U; | ||
|
|
||
| // Let's build the actual source code Clang needs and setup some utility | ||
| // objects. | ||
| std::string full_source = previous_lines.str() + line.str(); | ||
| llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_ids(new DiagnosticIDs()); | ||
| llvm::IntrusiveRefCntPtr<DiagnosticOptions> diags_opts( | ||
| new DiagnosticOptions()); | ||
| DiagnosticsEngine diags(diag_ids, diags_opts); | ||
| clang::SourceManager SM(diags, file_mgr); | ||
| auto buf = llvm::MemoryBuffer::getMemBuffer(full_source); | ||
|
|
||
| FileID FID = SM.createFileID(clang::SourceManager::Unowned, buf.get()); | ||
|
|
||
| // Let's just enable the latest ObjC and C++ which should get most tokens | ||
| // right. | ||
| LangOptions Opts; | ||
| Opts.ObjC2 = true; | ||
| Opts.CPlusPlus17 = true; | ||
| Opts.LineComment = true; | ||
|
|
||
| Lexer lex(FID, buf.get(), SM, Opts); | ||
| // The lexer should keep whitespace around. | ||
| lex.SetKeepWhitespaceMode(true); | ||
|
|
||
| // Keeps track if we have entered a PP directive. | ||
| bool in_pp_directive = false; | ||
|
|
||
| // True once we actually lexed the user provided line. | ||
| bool found_user_line = false; | ||
|
|
||
| Token token; | ||
| bool exit = false; | ||
| while (!exit) { | ||
| // Returns true if this is the last token we get from the lexer. | ||
| exit = lex.LexFromRawLexer(token); | ||
|
|
||
| bool invalid = false; | ||
| unsigned current_line_number = | ||
| SM.getSpellingLineNumber(token.getLocation(), &invalid); | ||
| if (current_line_number != line_number) | ||
| continue; | ||
| found_user_line = true; | ||
|
|
||
| // We don't need to print any tokens without a spelling line number. | ||
| if (invalid) | ||
| continue; | ||
|
|
||
| // Same as above but with the column number. | ||
| invalid = false; | ||
| unsigned start = SM.getSpellingColumnNumber(token.getLocation(), &invalid); | ||
| if (invalid) | ||
| continue; | ||
| // Column numbers start at 1, but indexes in our string start at 0. | ||
| --start; | ||
|
|
||
| // Annotations don't have a length, so let's skip them. | ||
| if (token.isAnnotation()) | ||
| continue; | ||
|
|
||
| // Extract the token string from our source code. | ||
| llvm::StringRef tok_str = line.substr(start, token.getLength()); | ||
|
|
||
| // If the token is just an empty string, we can skip all the work below. | ||
| if (tok_str.empty()) | ||
| continue; | ||
|
|
||
| // See how we are supposed to highlight this token. | ||
| HighlightStyle::ColorStyle color = | ||
| determineClangStyle(*this, token, tok_str, options, in_pp_directive); | ||
|
|
||
| written_bytes += color.Apply(result, tok_str); | ||
| } | ||
|
|
||
| // If we went over the whole file but couldn't find our own file, then | ||
| // somehow our setup was wrong. When we're in release mode we just give the | ||
| // user the normal line and pretend we don't know how to highlight it. In | ||
| // debug mode we bail out with an assert as this should never happen. | ||
| if (!found_user_line) { | ||
| result << line; | ||
| written_bytes += line.size(); | ||
| assert(false && "We couldn't find the user line in the input file?"); | ||
| } | ||
|
|
||
| return written_bytes; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| //===-- ClangHighlighter.h --------------------------------------*- C++ -*-===// | ||
| // | ||
| // The LLVM Compiler Infrastructure | ||
| // | ||
| // This file is distributed under the University of Illinois Open Source | ||
| // License. See LICENSE.TXT for details. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef liblldb_ClangHighlighter_h_ | ||
| #define liblldb_ClangHighlighter_h_ | ||
|
|
||
| // C Includes | ||
| // C++ Includes | ||
| // Other libraries and framework includes | ||
| #include "lldb/Utility/Stream.h" | ||
| #include "llvm/ADT/StringSet.h" | ||
|
|
||
| // Project includes | ||
| #include "lldb/Core/Highlighter.h" | ||
|
|
||
| namespace lldb_private { | ||
|
|
||
| class ClangHighlighter : public Highlighter { | ||
| llvm::StringSet<> keywords; | ||
|
|
||
| public: | ||
| ClangHighlighter(); | ||
| llvm::StringRef GetName() const override { return "clang"; } | ||
|
|
||
| std::size_t Highlight(const HighlightStyle &options, llvm::StringRef line, | ||
| llvm::StringRef previous_lines, | ||
| Stream &s) const override; | ||
|
|
||
| /// Returns true if the given string represents a keywords in any Clang | ||
| /// supported language. | ||
| bool isKeyword(llvm::StringRef token) const; | ||
| }; | ||
|
|
||
| } // namespace lldb_private | ||
|
|
||
| #endif // liblldb_ClangHighlighter_h_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| add_lldb_library(lldbPluginObjCPlusPlusLanguage PLUGIN | ||
| ObjCPlusPlusLanguage.cpp | ||
|
|
||
| LINK_LIBS | ||
| lldbCore | ||
| lldbTarget | ||
| lldbPluginClangCommon | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| add_subdirectory(CPlusPlus) | ||
| add_subdirectory(Highlighting) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| add_lldb_unittest(HighlighterTests | ||
| HighlighterTest.cpp | ||
|
|
||
| LINK_LIBS | ||
| lldbPluginCPlusPlusLanguage | ||
| lldbPluginObjCLanguage | ||
| lldbPluginObjCPlusPlusLanguage | ||
| lldbPluginJavaLanguage | ||
| lldbPluginOCamlLanguage | ||
| lldbPluginGoLanguage | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| //===-- HighlighterTest.cpp -------------------------------------*- C++ -*-===// | ||
| // | ||
| // The LLVM Compiler Infrastructure | ||
| // | ||
| // This file is distributed under the University of Illinois Open Source | ||
| // License. See LICENSE.TXT for details. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "gtest/gtest.h" | ||
|
|
||
| #include "lldb/Core/Highlighter.h" | ||
|
|
||
| #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" | ||
| #include "Plugins/Language/Go/GoLanguage.h" | ||
| #include "Plugins/Language/Java/JavaLanguage.h" | ||
| #include "Plugins/Language/OCaml/OCamlLanguage.h" | ||
| #include "Plugins/Language/ObjC/ObjCLanguage.h" | ||
| #include "Plugins/Language/ObjCPlusPlus/ObjCPlusPlusLanguage.h" | ||
|
|
||
| using namespace lldb_private; | ||
|
|
||
| namespace { | ||
| class HighlighterTest : public testing::Test { | ||
| public: | ||
| static void SetUpTestCase(); | ||
| static void TearDownTestCase(); | ||
| }; | ||
| } // namespace | ||
|
|
||
| void HighlighterTest::SetUpTestCase() { | ||
| // The HighlighterManager uses the language plugins under the hood, so we | ||
| // have to initialize them here for our test process. | ||
| CPlusPlusLanguage::Initialize(); | ||
| GoLanguage::Initialize(); | ||
| JavaLanguage::Initialize(); | ||
| ObjCLanguage::Initialize(); | ||
| ObjCPlusPlusLanguage::Initialize(); | ||
| OCamlLanguage::Initialize(); | ||
| } | ||
|
|
||
| void HighlighterTest::TearDownTestCase() { | ||
| CPlusPlusLanguage::Terminate(); | ||
| GoLanguage::Terminate(); | ||
| JavaLanguage::Terminate(); | ||
| ObjCLanguage::Terminate(); | ||
| ObjCPlusPlusLanguage::Terminate(); | ||
| OCamlLanguage::Terminate(); | ||
| } | ||
|
|
||
| static std::string getName(lldb::LanguageType type) { | ||
| HighlighterManager m; | ||
| return m.getHighlighterFor(type, "").GetName().str(); | ||
| } | ||
|
|
||
| static std::string getName(llvm::StringRef path) { | ||
| HighlighterManager m; | ||
| return m.getHighlighterFor(lldb::eLanguageTypeUnknown, path).GetName().str(); | ||
| } | ||
|
|
||
| TEST_F(HighlighterTest, HighlighterSelectionType) { | ||
| EXPECT_EQ(getName(lldb::eLanguageTypeC_plus_plus), "clang"); | ||
| EXPECT_EQ(getName(lldb::eLanguageTypeC_plus_plus_03), "clang"); | ||
| EXPECT_EQ(getName(lldb::eLanguageTypeC_plus_plus_11), "clang"); | ||
| EXPECT_EQ(getName(lldb::eLanguageTypeC_plus_plus_14), "clang"); | ||
| EXPECT_EQ(getName(lldb::eLanguageTypeObjC), "clang"); | ||
| EXPECT_EQ(getName(lldb::eLanguageTypeObjC_plus_plus), "clang"); | ||
|
|
||
| EXPECT_EQ(getName(lldb::eLanguageTypeUnknown), "none"); | ||
| EXPECT_EQ(getName(lldb::eLanguageTypeJulia), "none"); | ||
| EXPECT_EQ(getName(lldb::eLanguageTypeJava), "none"); | ||
| EXPECT_EQ(getName(lldb::eLanguageTypeHaskell), "none"); | ||
| } | ||
|
|
||
| TEST_F(HighlighterTest, HighlighterSelectionPath) { | ||
| EXPECT_EQ(getName("myfile.cc"), "clang"); | ||
| EXPECT_EQ(getName("moo.cpp"), "clang"); | ||
| EXPECT_EQ(getName("mar.cxx"), "clang"); | ||
| EXPECT_EQ(getName("foo.C"), "clang"); | ||
| EXPECT_EQ(getName("bar.CC"), "clang"); | ||
| EXPECT_EQ(getName("a/dir.CC"), "clang"); | ||
| EXPECT_EQ(getName("/a/dir.hpp"), "clang"); | ||
| EXPECT_EQ(getName("header.h"), "clang"); | ||
|
|
||
| EXPECT_EQ(getName(""), "none"); | ||
| EXPECT_EQ(getName("/dev/null"), "none"); | ||
| EXPECT_EQ(getName("Factory.java"), "none"); | ||
| EXPECT_EQ(getName("poll.py"), "none"); | ||
| EXPECT_EQ(getName("reducer.hs"), "none"); | ||
| } | ||
|
|
||
| TEST_F(HighlighterTest, FallbackHighlighter) { | ||
| HighlighterManager mgr; | ||
| const Highlighter &h = | ||
| mgr.getHighlighterFor(lldb::eLanguageTypePascal83, "foo.pas"); | ||
|
|
||
| HighlightStyle style; | ||
| style.identifier.Set("[", "]"); | ||
| style.semicolons.Set("<", ">"); | ||
|
|
||
| const char *code = "program Hello;"; | ||
| std::string output = h.Highlight(style, code); | ||
|
|
||
| EXPECT_STREQ(output.c_str(), code); | ||
| } | ||
|
|
||
| TEST_F(HighlighterTest, DefaultHighlighter) { | ||
| HighlighterManager mgr; | ||
| const Highlighter &h = mgr.getHighlighterFor(lldb::eLanguageTypeC, "main.c"); | ||
|
|
||
| HighlightStyle style; | ||
|
|
||
| const char *code = "int my_main() { return 22; } \n"; | ||
| std::string output = h.Highlight(style, code); | ||
|
|
||
| EXPECT_STREQ(output.c_str(), code); | ||
| } | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Tests highlighting with the Clang highlighter. | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| static std::string highlightC(llvm::StringRef code, HighlightStyle style) { | ||
| HighlighterManager mgr; | ||
| const Highlighter &h = mgr.getHighlighterFor(lldb::eLanguageTypeC, "main.c"); | ||
| return h.Highlight(style, code); | ||
| } | ||
|
|
||
| TEST_F(HighlighterTest, ClangEmptyInput) { | ||
| HighlightStyle s; | ||
| EXPECT_EQ("", highlightC("", s)); | ||
| } | ||
|
|
||
| TEST_F(HighlighterTest, ClangScalarLiterals) { | ||
| HighlightStyle s; | ||
| s.scalar_literal.Set("<scalar>", "</scalar>"); | ||
|
|
||
| EXPECT_EQ(" int i = <scalar>22</scalar>;", highlightC(" int i = 22;", s)); | ||
| } | ||
|
|
||
| TEST_F(HighlighterTest, ClangStringLiterals) { | ||
| HighlightStyle s; | ||
| s.string_literal.Set("<str>", "</str>"); | ||
|
|
||
| EXPECT_EQ("const char *f = 22 + <str>\"foo\"</str>;", | ||
| highlightC("const char *f = 22 + \"foo\";", s)); | ||
| } | ||
|
|
||
| TEST_F(HighlighterTest, ClangUnterminatedString) { | ||
| HighlightStyle s; | ||
| s.string_literal.Set("<str>", "</str>"); | ||
|
|
||
| EXPECT_EQ(" f = \"", highlightC(" f = \"", s)); | ||
| } | ||
|
|
||
| TEST_F(HighlighterTest, Keywords) { | ||
| HighlightStyle s; | ||
| s.keyword.Set("<k>", "</k>"); | ||
|
|
||
| EXPECT_EQ(" <k>return</k> 1; ", highlightC(" return 1; ", s)); | ||
| } | ||
|
|
||
| TEST_F(HighlighterTest, Colons) { | ||
| HighlightStyle s; | ||
| s.colon.Set("<c>", "</c>"); | ||
|
|
||
| EXPECT_EQ("foo<c>:</c><c>:</c>bar<c>:</c>", highlightC("foo::bar:", s)); | ||
| } | ||
|
|
||
| TEST_F(HighlighterTest, ClangBraces) { | ||
| HighlightStyle s; | ||
| s.braces.Set("<b>", "</b>"); | ||
|
|
||
| EXPECT_EQ("a<b>{</b><b>}</b>", highlightC("a{}", s)); | ||
| } | ||
|
|
||
| TEST_F(HighlighterTest, ClangSquareBrackets) { | ||
| HighlightStyle s; | ||
| s.square_brackets.Set("<sb>", "</sb>"); | ||
|
|
||
| EXPECT_EQ("a<sb>[</sb><sb>]</sb>", highlightC("a[]", s)); | ||
| } | ||
|
|
||
| TEST_F(HighlighterTest, ClangCommas) { | ||
| HighlightStyle s; | ||
| s.comma.Set("<comma>", "</comma>"); | ||
|
|
||
| EXPECT_EQ(" bool f = foo()<comma>,</comma> 1;", | ||
| highlightC(" bool f = foo(), 1;", s)); | ||
| } | ||
|
|
||
| TEST_F(HighlighterTest, ClangPPDirectives) { | ||
| HighlightStyle s; | ||
| s.pp_directive.Set("<pp>", "</pp>"); | ||
|
|
||
| EXPECT_EQ("<pp>#</pp><pp>include</pp><pp> </pp><pp>\"foo\"</pp><pp> </pp>//c", | ||
| highlightC("#include \"foo\" //c", s)); | ||
| } | ||
|
|
||
| TEST_F(HighlighterTest, ClangComments) { | ||
| HighlightStyle s; | ||
| s.comment.Set("<cc>", "</cc>"); | ||
|
|
||
| EXPECT_EQ(" <cc>/*com */</cc> <cc>// com /*n*/</cc>", | ||
| highlightC(" /*com */ // com /*n*/", s)); | ||
| } | ||
|
|
||
| TEST_F(HighlighterTest, ClangOperators) { | ||
| HighlightStyle s; | ||
| s.operators.Set("[", "]"); | ||
|
|
||
| EXPECT_EQ(" 1[+]2[/]a[*]f[&]x[|][~]l", highlightC(" 1+2/a*f&x|~l", s)); | ||
| } | ||
|
|
||
| TEST_F(HighlighterTest, ClangIdentifiers) { | ||
| HighlightStyle s; | ||
| s.identifier.Set("<id>", "</id>"); | ||
|
|
||
| EXPECT_EQ(" <id>foo</id> <id>c</id> = <id>bar</id>(); return 1;", | ||
| highlightC(" foo c = bar(); return 1;", s)); | ||
| } |