[PATCH] D95860: [clang][Frontend] Fix a crash in DiagnosticRenderer.

Balázs Kéri via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 3 08:34:21 PST 2021


balazske added a comment.

Probably it is not worth to find a better solution. In the case of CloneChecker the range starts and ends on different line that is not possible to print on a single line in the output.
This code could work:

  // Then, crawl the expansion chain for the end of the range.
  if (BeginFileID != EndFileID) {
    while (End.isMacroID() && !BeginLocsMap.count(EndFileID)) {
      auto Exp = SM->getImmediateExpansionRange(End);
      IsTokenRange = Exp.isTokenRange();
      End = Exp.getEnd();
      EndFileID = SM->getFileID(End);
    }
    if (End.isMacroID()) {
      Begin = BeginLocsMap[EndFileID];
      BeginFileID = EndFileID;
    } else if (BeginFileID != EndFileID) {
      assert(!BeginLocsMap.count(EndFileID));
      //if (SM->getIncludeLoc(BeginFileID).isValid() && SM->getIncludeLoc(EndFileID).isValid())
      //  continue;
      if (SM->getIncludeLoc(BeginFileID).isValid()) {
        while (Begin.isValid() && BeginFileID != EndFileID) {
          Begin = SM->getIncludeLoc(BeginFileID);
          BeginFileID = SM->getFileID(Begin);
        }
      } else if (SM->getIncludeLoc(EndFileID).isValid()) {
        while (End.isValid() && BeginFileID != EndFileID) {
          End = SM->getIncludeLoc(EndFileID);
          EndFileID = SM->getFileID(End);
        }
      } else {
        llvm_unreachable("Got unacceptable source range with begin and end in different translation unit?");
      }
    }
  }
  
  if (Begin.isInvalid() || End.isInvalid())
    continue;
  
  // Do the backtracking.

With this solution the following result is printed:

  llvm-project/clang/test/Analysis/copypaste/clone-end-in-other-file.cpp:9:3: warning: Duplicate code detected [alpha.clone.CloneChecker]
    if (i == 10) // expected-warning{{Duplicate code detected}}
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  llvm-project/clang/test/Analysis/copypaste/clone-end-in-other-file.cpp:11:3: note: Similar code here
    if (i == 10) // expected-note{{Similar code here}}
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1 warning generated.

This is not better than the result with the existing code:

  llvm-project/clang/test/Analysis/copypaste/clone-end-in-other-file.cpp:9:3: warning: Duplicate code detected [alpha.clone.CloneChecker]
    if (i == 10) // expected-warning{{Duplicate code detected}}
    ^
  llvm-project/clang/test/Analysis/copypaste/clone-end-in-other-file.cpp:11:3: note: Similar code here
    if (i == 10) // expected-note{{Similar code here}}
    ^
  1 warning generated.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D95860/new/

https://reviews.llvm.org/D95860



More information about the cfe-commits mailing list