brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.7 KiB · 622dbc5 Raw
99 lines · cpp
1//===- unittests/Frontend/TextDiagnosticTest.cpp - ------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "clang/Frontend/TextDiagnostic.h"10#include "clang/Basic/FileManager.h"11#include "clang/Basic/LangOptions.h"12#include "clang/Basic/SourceManager.h"13#include "llvm/Support/SmallVectorMemoryBuffer.h"14#include "gtest/gtest.h"15 16using namespace llvm;17using namespace clang;18 19namespace {20 21/// Prints a diagnostic with the given DiagnosticOptions and the given22/// SourceLocation and returns the printed diagnostic text.23static std::string PrintDiag(DiagnosticOptions &Opts, FullSourceLoc Loc) {24  std::string Out;25  llvm::raw_string_ostream OS(Out);26  clang::LangOptions LangOpts;27  // Owned by TextDiagnostic.28  TextDiagnostic Diag(OS, LangOpts, Opts);29  // Emit a dummy diagnostic that is just 'message'.30  Diag.emitDiagnostic(Loc, DiagnosticsEngine::Level::Warning, "message",31                      /*Ranges=*/{}, /*FixItHints=*/{});32  return Out;33}34 35TEST(TextDiagnostic, ShowLine) {36  // Create dummy FileManager and SourceManager.37  FileSystemOptions FSOpts;38  FileManager FileMgr(FSOpts);39  DiagnosticOptions DiagEngineOpts;40  DiagnosticsEngine DiagEngine(DiagnosticIDs::create(), DiagEngineOpts,41                               new IgnoringDiagConsumer());42  SourceManager SrcMgr(DiagEngine, FileMgr);43 44  // Create a dummy file with some contents to produce a test SourceLocation.45  const llvm::StringRef file_path = "main.cpp";46  const llvm::StringRef main_file_contents = "some\nsource\ncode\n";47  const clang::FileEntryRef fe = FileMgr.getVirtualFileRef(48      file_path,49      /*Size=*/static_cast<off_t>(main_file_contents.size()),50      /*ModificationTime=*/0);51 52  llvm::SmallVector<char, 64> buffer;53  buffer.append(main_file_contents.begin(), main_file_contents.end());54  auto file_contents = std::make_unique<llvm::SmallVectorMemoryBuffer>(55      std::move(buffer), file_path, /*RequiresNullTerminator=*/false);56  SrcMgr.overrideFileContents(fe, std::move(file_contents));57 58  // Create the actual file id and use it as the main file.59  clang::FileID fid =60      SrcMgr.createFileID(fe, SourceLocation(), clang::SrcMgr::C_User);61  SrcMgr.setMainFileID(fid);62 63  // Create the source location for the test diagnostic.64  FullSourceLoc Loc(SrcMgr.translateLineCol(fid, /*Line=*/1, /*Col=*/2),65                    SrcMgr);66 67  DiagnosticOptions DiagOpts;68  DiagOpts.ShowLine = true;69  DiagOpts.ShowColumn = true;70  // Hide printing the source line/caret to make the diagnostic shorter and it's71  // not relevant for this test.72  DiagOpts.ShowCarets = false;73  EXPECT_EQ("main.cpp:1:2: warning: message\n", PrintDiag(DiagOpts, Loc));74 75  // Check that ShowLine doesn't influence the Vi/MSVC diagnostic formats as its76  // a Clang-specific diagnostic option.77  DiagOpts.setFormat(TextDiagnosticFormat::Vi);78  DiagOpts.ShowLine = false;79  EXPECT_EQ("main.cpp +1:2: warning: message\n", PrintDiag(DiagOpts, Loc));80 81  DiagOpts.setFormat(TextDiagnosticFormat::MSVC);82  DiagOpts.ShowLine = false;83  EXPECT_EQ("main.cpp(1,2): warning: message\n", PrintDiag(DiagOpts, Loc));84 85  // Reset back to the Clang format.86  DiagOpts.setFormat(TextDiagnosticFormat::Clang);87 88  // Hide line number but show column.89  DiagOpts.ShowLine = false;90  EXPECT_EQ("main.cpp:2: warning: message\n", PrintDiag(DiagOpts, Loc));91 92  // Show line number but hide column.93  DiagOpts.ShowLine = true;94  DiagOpts.ShowColumn = false;95  EXPECT_EQ("main.cpp:1: warning: message\n", PrintDiag(DiagOpts, Loc));96}97 98} // anonymous namespace99