Commit Graph

204 Commits

Author SHA1 Message Date
Ted Kremenek 08037045f8 Refine SourceManager's isBeforeInTranslationUnit() cache to have more entries.
isBeforeInTranslationUnit() uses a cache to reduce the expensive work
to compute a common ancestor for two FileIDs.  This work is very
expensive, so even caching the latest used FileIDs was a big win.
A closer analysis of the cache before, however, shows that the cache
access pattern would oscillate between a working set of FileIDs, and
thus caching more pairs would be profitable.

This patch adds a side table for extending caching.  This side table
is bounded in size (experimentally determined in this case from
a simple Objective-C project), and when the table gets too large
we fall back to the single entry caching before as before.

On Sketch (a small example Objective-C project), this optimization
reduces -fsyntax-only time on SKTGraphicView.m by 5%.  This is
for a project that is already using PCH.

Fixes <rdar://problem/13299847>

llvm-svn: 176142
2013-02-27 00:00:26 +00:00
Benjamin Kramer 2999b77f1d Replace some typically large vectors with SmallVector.
This may seem counter-intuitive but the POD-like optimization helps when the
vectors grow into multimegabyte buffers. SmallVector calls realloc which knows
how to twiddle virtual memory bits and avoids large copies.

llvm-svn: 175906
2013-02-22 18:29:39 +00:00
David Blaikie 7a30dc53c5 Use None rather than Optional<T>() where possible.
llvm-svn: 175705
2013-02-21 01:47:18 +00:00
David Blaikie 05785d1622 Include llvm::Optional in clang/Basic/LLVM.h
Post-commit CR feedback from Jordan Rose regarding r175594.

llvm-svn: 175679
2013-02-20 22:23:23 +00:00
Matt Beaumont-Gay b1e71a7d0c Fix -Wunused-comparison for comparisons in arguments to function-like macros.
Previously, -Wunused-comparison ignored comparisons in both macro bodies and
macro arguments, but we would still emit a -Wunused-value warning for either.
Now we correctly emit -Wunused-comparison for expressions in macro arguments.

Also, add isMacroBodyExpansion to SourceManager, to go along with
isMacroArgExpansion.

llvm-svn: 172279
2013-01-12 00:54:16 +00:00
Craig Topper f3b839b063 Don't use SourceLineCache in getColumnNumber if LastLineNoResult is past the end of the cache. Fixes PR14570.
llvm-svn: 170281
2012-12-16 05:58:32 +00:00
Chandler Carruth 3a02247dc9 Sort all of Clang's files under 'lib', and fix up the broken headers
uncovered.

This required manually correcting all of the incorrect main-module
headers I could find, and running the new llvm/utils/sort_includes.py
script over the files.

I also manually added quite a few missing headers that were uncovered by
shuffling the order or moving headers up to be main-module-headers.

llvm-svn: 169237
2012-12-04 09:13:33 +00:00
Richard Smith 0b50cb79ad Add -cc1 option -fno-diagnostics-use-presumed-location, a handy mode for
working with preprocessed testcases. This causes source locations in
diagnostics to point at the spelling location instead of the presumed location,
while still keeping the semantic effects of the line directives (entering and
leaving system-header mode, primarily).

llvm-svn: 168004
2012-11-14 23:55:25 +00:00
Argyrios Kyrtzidis 73ccdb9c15 When associating file ranges of macro arguments with their
macro expansion ranges, make sure to check all the FileID
entries that are contained in the spelling range of the
expansion for the macro argument.

Fixes rdar://12537982

llvm-svn: 166359
2012-10-20 00:51:32 +00:00
Craig Topper 5e79ee087e Teach getColumnNumber to use the line cache to get the start of the line if its on the same line as the last call to getLineNumber. Prevents needing to scan backwards for the new line. Fixes PR14106.
llvm-svn: 166265
2012-10-19 04:40:38 +00:00
Ted Kremenek bd1d7fa460 Silence dead store warning. It is conceptually possible we will
add more code that references SourceFile, so removing the dead store
doesn't seem appropriate for the long term.

llvm-svn: 165837
2012-10-12 22:56:33 +00:00
Roman Divacky e637711ae0 Dont cast away const needlessly. Found by gcc48 -Wcast-qual.
llvm-svn: 163325
2012-09-06 15:59:27 +00:00
Argyrios Kyrtzidis 6d7833f1b6 Introduce a flag in SourceManager to treat non-system source files
as "volatile", meaning there's a high enough chance that they may
change while we are trying to use them.

This flag is only enabled by libclang.
Currently "volatile" source files will be stat'ed immediately
before opening them, because the file size stat info
may not be accurate since when we got it (e.g. from the PCH).
This avoids crashes when trying to reference mmap'ed memory
from a file whose size is not what we expect.

Note that there's still a window for a racing issue to occur
but the window for it should be way smaller than before.
We can consider later on to avoid mmap completely on such files.

rdar://11612916

llvm-svn: 160074
2012-07-11 20:59:04 +00:00
James Dennett 0bb4164ce8 Documentation cleanup: Delete a duplicated/malformed doc comment.
llvm-svn: 159628
2012-07-02 23:56:17 +00:00
Meador Inge cdc0057e3c Revert predefined decl tracking.
r158085 added some logic to track predefined declarations.  The main reason we
had predefined declarations in the input was because the __builtin_va_list
declarations were injected into the preprocessor input.  As of r158592 we 
explicitly build the __builtin_va_list declarations.  Therefore the predefined
decl tracking is no longer needed.

llvm-svn: 158732
2012-06-19 18:17:30 +00:00
Jordan Rose 8d63d5b8e6 Fix the location of the fixit for -Wnewline-eof.
It turns out SourceManager treating the "one-past-the-end" location as invalid,
but then failing to set the invalid flag properly.

llvm-svn: 158699
2012-06-19 03:09:38 +00:00
James Dennett 87a2acf1eb Documentation cleanup: escaped # characters in Doxygen comments.
llvm-svn: 158614
2012-06-17 03:22:59 +00:00
James Dennett 88b71e4121 Documentation cleanup: escape # in a Doxygen comment.
llvm-svn: 158549
2012-06-15 21:28:23 +00:00
Douglas Gregor 02c2dbf45e Switch LineTableInfo to use FileID instead of int for file references,
from Tom Honermann!

llvm-svn: 158211
2012-06-08 16:40:28 +00:00
Jordan Rose ccf43ca05c Add pedantic warning -Wempty-translation-unit (C11 6.9p1).
In standard C since C89, a 'translation-unit' is syntactically defined to have
at least one "external-declaration", which is either a decl or a function
definition. In Clang the latter gives us a declaration as well.

The tricky bit about this warning is that our predefines can contain external
declarations (__builtin_va_list and the 128-bit integer types). Therefore our
AST parser now makes sure we have at least one declaration that doesn't come
from the predefines buffer.

Also, remove bogus warning about empty source files. This doesn't catch source
files that only contain comments, and never fired anyway because of our
predefines.

PR12665 and <rdar://problem/9165548>

llvm-svn: 158085
2012-06-06 17:25:21 +00:00
Argyrios Kyrtzidis 6eec06d0d4 [PCH] When validating that the files coming from PCH did not change, also
validate that we didn't override the contents of any of such files.

If this is detected, emit a diagnostic error and recover gracefully
by using the contents of the original file that the PCH was built from.

Part of rdar://11305263

llvm-svn: 156107
2012-05-03 21:50:39 +00:00
Benjamin Kramer 543036a447 SourceManager: Vectorize ComputeLineNumbers for SSE2.
This method is very hot, it is called when emitting diagnostics, in -E mode
and for many #pragma handlers. It scans through the whole source file to
count newlines, records and caches them in a vector.

The speedup from vectorization isn't very large, as we fall back to bytewise
scanning when we hit a newline. There might be a way to avoid leaving the sse
loop but everything I tried didn't work out because a call to push_back
clobbers xmm registers.

About 2% speedup on average on "clang -E > /dev/null" of all .cpp files in
clang's lib/Sema.

llvm-svn: 154204
2012-04-06 20:49:55 +00:00
Argyrios Kyrtzidis 969fdfddd2 [PCH] Recover gracefully if the ASTReader detects that a file is different
from the one stored in the PCH/AST, while trying to load a SLocEntry.

We verify that all files of the PCH did not change before loading it but this is not enough because:

- The AST may have been 1) kept around, 2) to do queries on it.
- We may have 1) verified the PCH and 2) started parsing.

Between 1) and 2) files may change and we are going to have crashes because the rest of clang
cannot deal with the ASTReader failing to read a SLocEntry.

Handle this by recovering gracefully in such a case, by initializing the SLocEntry
with the info from the PCH/AST as well as reporting failure by the ASTReader.

rdar://10888929

llvm-svn: 151004
2012-02-20 23:58:07 +00:00
Argyrios Kyrtzidis e841c901de Fix bugs in SourceManager::computeMacroArgsCache() and add a unit test for it.
llvm-svn: 147057
2011-12-21 16:56:35 +00:00
Argyrios Kyrtzidis 2403797eec For SourceManager::isBeforeInTranslationUnit(), have it consider macro arg expanded
token locations as coming before the closing ')' of a function macro expansion.

Include a unit test for SourceManager.

llvm-svn: 147056
2011-12-21 16:56:29 +00:00
Dylan Noblesmith 2a8bc15230 SourceManager: use getBufferSize()
Forming an out of bounds pointer to check if it's out
of bounds was undefined behavior.

llvm-svn: 146861
2011-12-19 08:51:05 +00:00
Argyrios Kyrtzidis af41b3f0f2 In SourceManager::~SourceManager do a sanity check to make sure we
don't try to destruct a null ContentCache.

rdar://10567159

llvm-svn: 146707
2011-12-15 23:37:55 +00:00
Argyrios Kyrtzidis cc6107df08 In ContentCache::replaceBuffer, add sanity check to make sure that we do not free a buffer
and then continue using it. rdar://10359140.

llvm-svn: 146308
2011-12-10 01:38:26 +00:00
Argyrios Kyrtzidis 6c8d29f7d1 Add a sanity check in SourceManager::getColumnNumber, make sure
we don't try to access beyond the buffer.

llvm-svn: 146305
2011-12-10 00:30:38 +00:00
Douglas Gregor 9dc3212f98 Implement (de-)serialization of the buffer contents for an overridden
file in the source manager. This allows us to properly create and use
modules described by module map files without umbrella headers (or
with incompletely umbrella headers). More generally, we can actually
build a PCH file that makes use of  file -> buffer remappings, which
could be useful in libclang in the future.

llvm-svn: 144830
2011-11-16 20:05:18 +00:00
Argyrios Kyrtzidis 8edffaa611 Use assert(0) instead of duplicating the check, suggestion by Anna.
llvm-svn: 142886
2011-10-25 00:29:44 +00:00
Argyrios Kyrtzidis 7f6b029c4e Introduce SourceManager::getFileLoc which returns a file location
taking into account macro arguments.

llvm-svn: 141771
2011-10-12 07:07:40 +00:00
Argyrios Kyrtzidis 25029d499e Make sure SourceManager::getFileIDLoaded doesn't hang in release build because of invalid passed parameter.
rdar://10210140

llvm-svn: 141048
2011-10-03 23:43:01 +00:00
Argyrios Kyrtzidis 04a6e5f82e Break SourceManager::translateFileLineCol into translateFile, no functionality change.
llvm-svn: 140610
2011-09-27 17:22:25 +00:00
Argyrios Kyrtzidis 4bdd6aa198 Associate the macro arguments location map with a FileID instead
of a ContentCache, since multiple FileIDs can have the same ContentCache
but the expanded macro arguments locations will be different.

llvm-svn: 140521
2011-09-26 08:01:50 +00:00
David Blaikie 9c902b5502 Rename Diagnostic to DiagnosticsEngine as per issue 5397
llvm-svn: 140478
2011-09-25 23:23:43 +00:00
Argyrios Kyrtzidis 7c2b28a16f In SourceManager::translateLineCol, handle the case where we are pointing
directly at the end of the source file.

llvm-svn: 140192
2011-09-20 22:14:54 +00:00
Argyrios Kyrtzidis 7c06d8666b [libclang] When getting a source location from a file:line:col triplet
check whether the requested location points inside the precompiled preamble,
in which case the returned source location will be a "loaded" one.

llvm-svn: 140060
2011-09-19 20:40:35 +00:00
Argyrios Kyrtzidis 532c5196b0 Break SourceManager::translateFileLineCol into translateLineCol that returns the
source location of line:col of a specific FileID.

llvm-svn: 140059
2011-09-19 20:40:29 +00:00
Argyrios Kyrtzidis e6e67deeed Rename SourceLocation::getFileLocWithOffset -> getLocWithOffset.
It already works (and is useful with) macro locs as well.

llvm-svn: 140057
2011-09-19 20:40:19 +00:00
Argyrios Kyrtzidis b4199b659b For SourceManager::isBeforeInTranslationUnit, a location pointing
inside a macro argument should be regarded as coming before
the location of the expanded tokens.

llvm-svn: 140053
2011-09-19 20:39:57 +00:00
Argyrios Kyrtzidis 18c88a96eb Remove one SourceManager::isInFileID overload and use isOffsetInFileID for the other.
llvm-svn: 138381
2011-08-23 21:02:45 +00:00
Argyrios Kyrtzidis 2797df6a24 Amend r138129 (reduction of SLocEntries) which introduced performance regression due
to increased calls to SourceManager::getFileID. (rdar://9992664)

Use a slightly different approach that is more efficient both in terms of speed
(no extra getFileID calls) and in SLocEntries reduction.

Comparing pre-r138129 and this patch we get:

For compiling SemaExpr.cpp reduction of SLocEntries by 26%.
For the boost enum library:
  -SLocEntries -34% (note that this was -5% for r138129)
  -Memory consumption -50%
  -PCH size -31%

Reduced SLocEntries also benefit the hot function SourceManager::getFileID,
evident by the reduced "FileID scans".

llvm-svn: 138380
2011-08-23 21:02:41 +00:00
Argyrios Kyrtzidis 296374b501 Move a couple of SourceManager functions out-of-line; no functionality change.
llvm-svn: 138375
2011-08-23 21:02:28 +00:00
Argyrios Kyrtzidis 61ef3db222 Boost the efficiency of SourceManager::getMacroArgExpandedLocation.
Currently getMacroArgExpandedLocation is very inefficient and for the case
of a location pointing at the main file it will end up checking almost all of
the SLocEntries. Make it faster:

-Use a map of macro argument chunks to their expanded source location. The map
 is for a single source file, it's stored in the file's ContentCache and lazily
 computed, like the source lines cache.
-In SLocEntry's FileInfo add an 'unsigned NumCreatedFIDs' field that keeps track
 of the number of FileIDs (files and macros) that were created during preprocessing
 of that particular file SLocEntry. This is useful when computing the macro argument
 map in skipping included files while scanning for macro arg FileIDs that lexed from
 a specific source file. Due to padding, the new field does not increase the size
 of SLocEntry.

llvm-svn: 138225
2011-08-21 23:33:04 +00:00
Argyrios Kyrtzidis 34d729d46d Refactor common functionality into SourceManager::getFileIDSize, no functionality change.
llvm-svn: 138127
2011-08-19 22:34:01 +00:00
Argyrios Kyrtzidis 92a47bd997 Introduce SourceManager::getMacroArgExpandedLocation function.
If we pass it a source location that points inside a function macro argument,
the returned location will be the macro location in which the argument was expanded.
If a macro argument is used multiple times, the expanded location will
be at the first expansion of the argument.
 e.g.
   MY_MACRO(foo);
             ^
Passing a file location pointing at 'foo', will yield a macro location
where 'foo' was expanded into.

Make SourceManager::getLocation call getMacroArgExpandedLocation as well.

llvm-svn: 137794
2011-08-17 00:31:20 +00:00
Argyrios Kyrtzidis ac199bffab Make SourceManager::isBeforeInTranslationUnit handle macro locations correctly.
llvm-svn: 137793
2011-08-17 00:31:18 +00:00
Ted Kremenek 43e0c4a887 Change SourceManager::getDataStructureSizes() to use llvm::capacity_in_bytes().
llvm-svn: 136236
2011-07-27 18:41:16 +00:00
Ted Kremenek 120992ad81 clang_getCXTUResourceUsage: Report memory used by data structures in SourceManager.
llvm-svn: 136189
2011-07-26 23:46:06 +00:00