Files
carbon-lang/explorer/common/source_location.h
T
Prabhat SachdevaandRichard Smith c0d18a62eb Trace output filtering based on file context (#2916)
To filter based on file contexts, you can use `-trace_file_context=...`
flag along with `--trace_file=...` flag. These are the following options
you can pass to the `-trace_file_context=...` flag (you can also pass
them as list by separating them with comma).
1. `main`: Include trace output for file containing the main function.
2. `prelude`: Include trace output for prelude.
3. `import`: Include trace output for imports.
4. `include`: Include trace output for all.

If the flag isn't used or non of the options are passed, by default only
main file context is traced.

File contexts are distinguished based upon the source location by
passing the source location to `TraceStream::is_enabled(..)` as
arguments. If no arguments are passed, file context is
`FileContext::Unknown` and for now it will add the trace from unknown
file context.

Note: `import` option currently doesn't work as imports aren't supported
yet.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2023-07-07 22:17:34 +00:00

61 lines
1.9 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_EXPLORER_COMMON_SOURCE_LOCATION_H_
#define CARBON_EXPLORER_COMMON_SOURCE_LOCATION_H_
#include <string>
#include <string_view>
#include "common/ostream.h"
#include "explorer/common/nonnull.h"
namespace Carbon {
class SourceLocation {
public:
// Produce a source location that is known to not be used, because it is fed
// into an operation that creates no AST nodes and whose diagnostics are
// discarded.
static auto DiagnosticsIgnored() -> SourceLocation {
return SourceLocation("", 0);
}
// The filename should be eternal or arena-allocated to eliminate copies.
constexpr SourceLocation(const char* filename, int line_num)
: filename_(filename), line_num_(line_num) {}
SourceLocation(Nonnull<const std::string*> filename, int line_num)
: filename_(*filename), line_num_(line_num) {}
SourceLocation(const SourceLocation&) = default;
SourceLocation(SourceLocation&&) = default;
auto operator=(const SourceLocation&) -> SourceLocation& = default;
auto operator=(SourceLocation&&) -> SourceLocation& = default;
auto operator==(SourceLocation other) const -> bool {
return filename_ == other.filename_ && line_num_ == other.line_num_;
}
auto filename() const -> std::string_view { return filename_; }
void Print(llvm::raw_ostream& out) const {
out << filename_ << ":" << line_num_;
}
auto ToString() const -> std::string {
std::string result;
llvm::raw_string_ostream out(result);
Print(out);
return result;
}
LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
private:
std::string_view filename_;
int line_num_;
};
} // namespace Carbon
#endif // CARBON_EXPLORER_COMMON_SOURCE_LOCATION_H_