TEST(FileSpecificDiagnosticConsumer, NotesAreAttachedToErrorsEvenAcrossFiles) {
  SourceManager sourceMgr;
  //                                             01234
  unsigned bufferA = sourceMgr.addMemBufferCopy("abcde", "A");
  unsigned bufferB = sourceMgr.addMemBufferCopy("vwxyz", "B");

  SourceLoc frontOfA = sourceMgr.getLocForOffset(bufferA, 0);
  SourceLoc middleOfA = sourceMgr.getLocForOffset(bufferA, 2);
  SourceLoc backOfA = sourceMgr.getLocForOffset(bufferA, 4);

  SourceLoc frontOfB = sourceMgr.getLocForOffset(bufferB, 0);
  SourceLoc middleOfB = sourceMgr.getLocForOffset(bufferB, 2);
  SourceLoc backOfB = sourceMgr.getLocForOffset(bufferB, 4);

  ExpectedDiagnostic expectedA[] = {
    {frontOfA, "error"},
    {middleOfB, "note"},
    {backOfA, "note"},
    {frontOfA, "error"},
    {middleOfB, "note"},
    {backOfA, "note"},
  };
  ExpectedDiagnostic expectedB[] = {
    {frontOfB, "error"},
    {middleOfA, "note"},
    {backOfB, "note"},
  };

  auto consumerA = llvm::make_unique<ExpectationDiagnosticConsumer>(
      nullptr, expectedA);
  auto consumerB = llvm::make_unique<ExpectationDiagnosticConsumer>(
      consumerA.get(), expectedB);

  SmallVector<FileSpecificDiagnosticConsumer::Subconsumer, 2> consumers;
  consumers.emplace_back("A", std::move(consumerA));
  consumers.emplace_back("B", std::move(consumerB));

  auto topConsumer =
      FileSpecificDiagnosticConsumer::consolidateSubconsumers(consumers);
  topConsumer->handleDiagnostic(sourceMgr, frontOfA, DiagnosticKind::Error,
                                "error", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, middleOfB, DiagnosticKind::Note,
                                "note", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, backOfA, DiagnosticKind::Note,
                                "note", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, frontOfB, DiagnosticKind::Error,
                                "error", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, middleOfA, DiagnosticKind::Note,
                                "note", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, backOfB, DiagnosticKind::Note,
                                "note", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, frontOfA, DiagnosticKind::Error,
                                "error", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, middleOfB, DiagnosticKind::Note,
                                "note", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, backOfA, DiagnosticKind::Note,
                                "note", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->finishProcessing();
}
TEST(FileSpecificDiagnosticConsumer,
     ErrorsInUnaffiliatedFilesGoToEveryConsumer) {
  SourceManager sourceMgr;
  //                                             01234
  unsigned bufferA = sourceMgr.addMemBufferCopy("abcde", "A");
  unsigned bufferB = sourceMgr.addMemBufferCopy("vwxyz", "B");

  SourceLoc frontOfA = sourceMgr.getLocForOffset(bufferA, 0);
  SourceLoc middleOfA = sourceMgr.getLocForOffset(bufferA, 2);
  SourceLoc backOfA = sourceMgr.getLocForOffset(bufferA, 4);

  SourceLoc frontOfB = sourceMgr.getLocForOffset(bufferB, 0);
  SourceLoc middleOfB = sourceMgr.getLocForOffset(bufferB, 2);
  SourceLoc backOfB = sourceMgr.getLocForOffset(bufferB, 4);

  ExpectedDiagnostic expectedA[] = {
    {frontOfA, "front"},
    {frontOfB, "front"},
    {middleOfA, "middle"},
    {middleOfB, "middle"},
    {backOfA, "back"},
    {backOfB, "back"}
  };
  ExpectedDiagnostic expectedUnaffiliated[] = {
    {frontOfB, "front"},
    {middleOfB, "middle"},
    {backOfB, "back"}
  };

  auto consumerA = llvm::make_unique<ExpectationDiagnosticConsumer>(
      nullptr, expectedA);
  auto consumerUnaffiliated = llvm::make_unique<ExpectationDiagnosticConsumer>(
      consumerA.get(), expectedUnaffiliated);

  SmallVector<FileSpecificDiagnosticConsumer::Subconsumer, 2> consumers;
  consumers.emplace_back("A", std::move(consumerA));
  consumers.emplace_back("", std::move(consumerUnaffiliated));

  auto topConsumer =
      FileSpecificDiagnosticConsumer::consolidateSubconsumers(consumers);
  topConsumer->handleDiagnostic(sourceMgr, frontOfA, DiagnosticKind::Error,
                                "front", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, frontOfB, DiagnosticKind::Error,
                                "front", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, middleOfA, DiagnosticKind::Error,
                                "middle", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, middleOfB, DiagnosticKind::Error,
                                "middle", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, backOfA, DiagnosticKind::Error,
                                "back", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, backOfB, DiagnosticKind::Error,
                                "back", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->finishProcessing();
}
static std::vector<SourceLoc> tokenize(SourceManager &SM, StringRef Source) {
  unsigned ID = SM.addMemBufferCopy(Source);
  const MemoryBuffer *Buf = SM.getLLVMSourceMgr().getMemoryBuffer(ID);

  SourceLoc BeginLoc(SMLoc::getFromPointer(Buf->getBuffer().begin()));
  std::vector<SourceLoc> Result;
  Result.push_back(BeginLoc);
  for (unsigned i = 1, e = Source.size(); i != e; ++i) {
    if (Source[i - 1] == ' ')
      Result.push_back(BeginLoc.getAdvancedLoc(i));
  }
  return Result;
}
TEST(FileSpecificDiagnosticConsumer, ErrorsWithLocationsGoToExpectedConsumers) {
  SourceManager sourceMgr;
  //                                             01234
  unsigned bufferA = sourceMgr.addMemBufferCopy("abcde", "A");
  unsigned bufferB = sourceMgr.addMemBufferCopy("vwxyz", "B");

  SourceLoc frontOfA = sourceMgr.getLocForOffset(bufferA, 0);
  SourceLoc middleOfA = sourceMgr.getLocForOffset(bufferA, 2);
  SourceLoc backOfA = sourceMgr.getLocForOffset(bufferA, 4);

  SourceLoc frontOfB = sourceMgr.getLocForOffset(bufferB, 0);
  SourceLoc middleOfB = sourceMgr.getLocForOffset(bufferB, 2);
  SourceLoc backOfB = sourceMgr.getLocForOffset(bufferB, 4);

  ExpectedDiagnostic expectedA[] = {
    {frontOfA, "front"},
    {middleOfA, "middle"},
    {backOfA, "back"},
  };
  ExpectedDiagnostic expectedB[] = {
    {frontOfB, "front"},
    {middleOfB, "middle"},
    {backOfB, "back"}
  };

  auto consumerA = llvm::make_unique<ExpectationDiagnosticConsumer>(
      nullptr, expectedA);
  auto consumerB = llvm::make_unique<ExpectationDiagnosticConsumer>(
      consumerA.get(), expectedB);

  SmallVector<FileSpecificDiagnosticConsumer::ConsumerPair, 2> consumers;
  consumers.emplace_back("A", std::move(consumerA));
  consumers.emplace_back("B", std::move(consumerB));

  FileSpecificDiagnosticConsumer topConsumer(consumers);
  topConsumer.handleDiagnostic(sourceMgr, frontOfA, DiagnosticKind::Error,
                               "front", {}, DiagnosticInfo());
  topConsumer.handleDiagnostic(sourceMgr, frontOfB, DiagnosticKind::Error,
                               "front", {}, DiagnosticInfo());
  topConsumer.handleDiagnostic(sourceMgr, middleOfA, DiagnosticKind::Error,
                               "middle", {}, DiagnosticInfo());
  topConsumer.handleDiagnostic(sourceMgr, middleOfB, DiagnosticKind::Error,
                               "middle", {}, DiagnosticInfo());
  topConsumer.handleDiagnostic(sourceMgr, backOfA, DiagnosticKind::Error,
                               "back", {}, DiagnosticInfo());
  topConsumer.handleDiagnostic(sourceMgr, backOfB, DiagnosticKind::Error,
                               "back", {}, DiagnosticInfo());
  topConsumer.finishProcessing();
}
TEST(FileSpecificDiagnosticConsumer,
     NotesWithInvalidLocsAreStillAttachedToErrors) {
  SourceManager sourceMgr;
  //                                             01234
  unsigned bufferA = sourceMgr.addMemBufferCopy("abcde", "A");
  unsigned bufferB = sourceMgr.addMemBufferCopy("vwxyz", "B");

  SourceLoc frontOfA = sourceMgr.getLocForBufferStart(bufferA);
  SourceLoc frontOfB = sourceMgr.getLocForBufferStart(bufferB);

  ExpectedDiagnostic expectedA[] = {
    {frontOfA, "error"},
    {SourceLoc(), "note"},
    {frontOfB, "error"},
    {SourceLoc(), "note"},
    {frontOfA, "error"},
    {SourceLoc(), "note"},
  };
  ExpectedDiagnostic expectedUnaffiliated[] = {
    {frontOfB, "error"},
    {SourceLoc(), "note"},
  };

  auto consumerA = llvm::make_unique<ExpectationDiagnosticConsumer>(
      nullptr, expectedA);
  auto consumerUnaffiliated = llvm::make_unique<ExpectationDiagnosticConsumer>(
      consumerA.get(), expectedUnaffiliated);

  SmallVector<FileSpecificDiagnosticConsumer::Subconsumer, 2> consumers;
  consumers.emplace_back("A", std::move(consumerA));
  consumers.emplace_back("", std::move(consumerUnaffiliated));

  auto topConsumer =
      FileSpecificDiagnosticConsumer::consolidateSubconsumers(consumers);
  topConsumer->handleDiagnostic(sourceMgr, frontOfA, DiagnosticKind::Error,
                                "error", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, SourceLoc(), DiagnosticKind::Note,
                                "note", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, frontOfB, DiagnosticKind::Error,
                                "error", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, SourceLoc(), DiagnosticKind::Note,
                                "note", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, frontOfA, DiagnosticKind::Error,
                                "error", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, SourceLoc(), DiagnosticKind::Note,
                                "note", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->finishProcessing();
}
TEST(FileSpecificDiagnosticConsumer, SubConsumersFinishInOrder) {
  SourceManager sourceMgr;
  (void)sourceMgr.addMemBufferCopy("abcde", "A");
  (void)sourceMgr.addMemBufferCopy("vwxyz", "B");

  auto consumerA = llvm::make_unique<ExpectationDiagnosticConsumer>(
      nullptr, None);
  auto consumerUnaffiliated = llvm::make_unique<ExpectationDiagnosticConsumer>(
      consumerA.get(), None);

  SmallVector<FileSpecificDiagnosticConsumer::ConsumerPair, 2> consumers;
  consumers.emplace_back("A", std::move(consumerA));
  consumers.emplace_back("", std::move(consumerUnaffiliated));

  FileSpecificDiagnosticConsumer topConsumer(consumers);
  topConsumer.finishProcessing();
}
Exemple #7
0
std::string ide::extractPlainTextFromComment(const StringRef Text) {
  LangOptions LangOpts;
  SourceManager SourceMgr;
  auto Tokens = swift::tokenize(LangOpts, SourceMgr,
                                SourceMgr.addMemBufferCopy(Text));
  std::vector<SingleRawComment> Comments;
  Comments.reserve(Tokens.size());
  for (auto &Tok : Tokens) {
    if (Tok.is(tok::comment)) {
      Comments.push_back(SingleRawComment(Tok.getText(), 0));
    }
  }
  if (Comments.empty())
    return {};

  RawComment Comment(Comments);
  swift::markup::MarkupContext MC;
  return MC.getLineList(Comment).str();
}
TEST(FileSpecificDiagnosticConsumer, InvalidLocDiagsGoToEveryConsumer) {
  SourceManager sourceMgr;
  (void)sourceMgr.addMemBufferCopy("abcde", "A");
  (void)sourceMgr.addMemBufferCopy("vwxyz", "B");

  ExpectedDiagnostic expected[] = { {SourceLoc(), "dummy"} };
  auto consumerA = llvm::make_unique<ExpectationDiagnosticConsumer>(
      nullptr, expected);
  auto consumerUnaffiliated = llvm::make_unique<ExpectationDiagnosticConsumer>(
      consumerA.get(), expected);

  SmallVector<FileSpecificDiagnosticConsumer::ConsumerPair, 2> consumers;
  consumers.emplace_back("A", std::move(consumerA));
  consumers.emplace_back("", std::move(consumerUnaffiliated));

  FileSpecificDiagnosticConsumer topConsumer(consumers);
  topConsumer.handleDiagnostic(sourceMgr, SourceLoc(), DiagnosticKind::Error,
                               "dummy", {}, DiagnosticInfo());
  topConsumer.finishProcessing();
}
TEST(FileSpecificDiagnosticConsumer, WarningsAndRemarksAreTreatedLikeErrors) {
  SourceManager sourceMgr;
  //                                             01234
  unsigned bufferA = sourceMgr.addMemBufferCopy("abcde", "A");
  unsigned bufferB = sourceMgr.addMemBufferCopy("vwxyz", "B");

  SourceLoc frontOfA = sourceMgr.getLocForBufferStart(bufferA);
  SourceLoc frontOfB = sourceMgr.getLocForBufferStart(bufferB);

  ExpectedDiagnostic expectedA[] = {
    {frontOfA, "warning"},
    {frontOfB, "warning"},
    {frontOfA, "remark"},
    {frontOfB, "remark"},
  };
  ExpectedDiagnostic expectedUnaffiliated[] = {
    {frontOfB, "warning"},
    {frontOfB, "remark"},
  };

  auto consumerA = llvm::make_unique<ExpectationDiagnosticConsumer>(
      nullptr, expectedA);
  auto consumerUnaffiliated = llvm::make_unique<ExpectationDiagnosticConsumer>(
      consumerA.get(), expectedUnaffiliated);

  SmallVector<FileSpecificDiagnosticConsumer::Subconsumer, 2> consumers;
  consumers.emplace_back("A", std::move(consumerA));
  consumers.emplace_back("", std::move(consumerUnaffiliated));

  auto topConsumer =
      FileSpecificDiagnosticConsumer::consolidateSubconsumers(consumers);
  topConsumer->handleDiagnostic(sourceMgr, frontOfA, DiagnosticKind::Warning,
                                "warning", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, frontOfB, DiagnosticKind::Warning,
                                "warning", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, frontOfA, DiagnosticKind::Remark,
                                "remark", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->handleDiagnostic(sourceMgr, frontOfB, DiagnosticKind::Remark,
                                "remark", {}, DiagnosticInfo(), SourceLoc());
  topConsumer->finishProcessing();
}
TEST(FileSpecificDiagnosticConsumer, NotesAreAttachedToWarningsAndRemarks) {
  SourceManager sourceMgr;
  //                                             01234
  unsigned bufferA = sourceMgr.addMemBufferCopy("abcde", "A");
  unsigned bufferB = sourceMgr.addMemBufferCopy("vwxyz", "B");

  SourceLoc frontOfA = sourceMgr.getLocForOffset(bufferA, 0);
  SourceLoc middleOfA = sourceMgr.getLocForOffset(bufferA, 2);
  SourceLoc backOfA = sourceMgr.getLocForOffset(bufferA, 4);

  SourceLoc frontOfB = sourceMgr.getLocForOffset(bufferB, 0);
  SourceLoc middleOfB = sourceMgr.getLocForOffset(bufferB, 2);
  SourceLoc backOfB = sourceMgr.getLocForOffset(bufferB, 4);

  ExpectedDiagnostic expectedA[] = {
    {frontOfA, "warning"},
    {middleOfA, "note"},
    {backOfA, "note"},
    {frontOfB, "warning"},
    {middleOfB, "note"},
    {backOfB, "note"},
    {frontOfA, "remark"},
    {middleOfA, "note"},
    {backOfA, "note"},
  };
  ExpectedDiagnostic expectedUnaffiliated[] = {
    {frontOfB, "warning"},
    {middleOfB, "note"},
    {backOfB, "note"},
  };

  auto consumerA = llvm::make_unique<ExpectationDiagnosticConsumer>(
      nullptr, expectedA);
  auto consumerUnaffiliated = llvm::make_unique<ExpectationDiagnosticConsumer>(
      consumerA.get(), expectedUnaffiliated);

  SmallVector<FileSpecificDiagnosticConsumer::ConsumerPair, 2> consumers;
  consumers.emplace_back("A", std::move(consumerA));
  consumers.emplace_back("", std::move(consumerUnaffiliated));

  FileSpecificDiagnosticConsumer topConsumer(consumers);
  topConsumer.handleDiagnostic(sourceMgr, frontOfA, DiagnosticKind::Warning,
                               "warning", {}, DiagnosticInfo());
  topConsumer.handleDiagnostic(sourceMgr, middleOfA, DiagnosticKind::Note,
                               "note", {}, DiagnosticInfo());
  topConsumer.handleDiagnostic(sourceMgr, backOfA, DiagnosticKind::Note,
                               "note", {}, DiagnosticInfo());
  topConsumer.handleDiagnostic(sourceMgr, frontOfB, DiagnosticKind::Warning,
                               "warning", {}, DiagnosticInfo());
  topConsumer.handleDiagnostic(sourceMgr, middleOfB, DiagnosticKind::Note,
                               "note", {}, DiagnosticInfo());
  topConsumer.handleDiagnostic(sourceMgr, backOfB, DiagnosticKind::Note,
                               "note", {}, DiagnosticInfo());
  topConsumer.handleDiagnostic(sourceMgr, frontOfA, DiagnosticKind::Remark,
                               "remark", {}, DiagnosticInfo());
  topConsumer.handleDiagnostic(sourceMgr, middleOfA, DiagnosticKind::Note,
                               "note", {}, DiagnosticInfo());
  topConsumer.handleDiagnostic(sourceMgr, backOfA, DiagnosticKind::Note,
                               "note", {}, DiagnosticInfo());
  topConsumer.finishProcessing();
}